Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting typescript types in dist #3

Merged
merged 13 commits into from
Oct 30, 2018
Merged
45 changes: 27 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"test": "yarn run jest --coverage --coverageReporters=text-lcov"
},
"files": [
"dist",
"index.d.ts"
"dist"
],
"keywords": [
"redux",
Expand All @@ -31,29 +30,32 @@
"type": "git",
"url": "https://github.com/extendi/beccaccino"
},
"typings": "./index.d.ts",
"typings": "./dist/index.d.ts",
"peerDependencies": {
"axios": "^0.18.0",
"redux": "^4.0.1"
},
"devDependencies": {
"@types/jest": "^23.3.0",
"@types/uuid": "^3.4.3",
"awesome-typescript-loader": "^5.2.0",
"@types/jest": "^23.3.8",
"@types/uuid": "^3.4.4",
"awesome-typescript-loader": "^5.2.1",
"coveralls": "^3.0.2",
"jest": "^23.4.1",
"redux": "^4.0.0",
"source-map-loader": "^0.2.3",
"ts-jest": "^23.0.1",
"ts-loader": "^4.4.2",
"jest": "^23.6.0",
"terser-webpack-plugin": "^1.1.0",
"ts-jest": "^23.10.4",
"ts-loader": "^5.2.2",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"tslint": "^5.11.0",
"tslint-config-airbnb": "^5.9.2",
"typescript": "^2.9.2",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.16.2",
"webpack-cli": "^3.1.0"
"tslint-config-airbnb": "^5.11.0",
"typescript": "^3.1.3",
"webpack": "^4.23.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"axios": "^0.18.0",
"path-to-regexp": "^2.2.1",
"uuid": "^3.3.2"
"path-to-regexp": "^2.4.0",
"uuid": "^3.3.2",
"redux": "^4.0.1"
},
"jest": {
"transform": {
Expand All @@ -62,6 +64,13 @@
"moduleNameMapper": {
"^@lib/(.*)$": "<rootDir>/src/$1"
},
"globals": {
"ts-jest": {
"diagnostics": {
"warnOnly": true
}
}
},
"testEnvironment": "node",
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
8 changes: 3 additions & 5 deletions src/Beccaccino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { EndpointConfig, Endpoint, BindedAction } from '@lib/endpoint';
import { REDUX_HTTP_ACTION_SIGNATURE, REDUX_HTTP_CLIENT_REQUEST } from '@lib/redux-http';
import { EndpointConfig, Endpoint, BindedAction } from './endpoint';
import { REDUX_HTTP_ACTION_SIGNATURE, REDUX_HTTP_CLIENT_REQUEST } from './redux-http';
import { v4 as uuid } from 'uuid';

export type EndpointMap = {
Expand Down Expand Up @@ -34,7 +34,7 @@ class ReduxHttpClient {
}
}

const beccaccino = (() => {
export const beccaccino = (() => {
let clientInstance: ReduxHttpClient = null;
return {
configure: (
Expand All @@ -55,5 +55,3 @@ const beccaccino = (() => {
},
};
})();

export default beccaccino;
2 changes: 1 addition & 1 deletion src/__tests__/Beccaccino.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Beccaccino from '@lib/Beccaccino';
import { Beccaccino } from '../';

const clientInstance = Beccaccino.configure({}, [
{ name: 'getPippo', path: 'https://google.com', method: 'get' },
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
beccaccinoMiddleware,
beccaccinoReducer,
BECCACCINO_REDUCER_NAME,
} from '@lib/index';
} from '../';

import {
createStore,
Expand Down
36 changes: 31 additions & 5 deletions src/endpoint/Endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import { BindRequest, BindedAction } from '@lib/endpoint';
import requestHandler from '@lib/endpoint/requestHandler';
import { BindedActionPayload } from '../redux-http';
import { compile as compilePath } from 'path-to-regexp';
import { v4 as uuid } from 'uuid';
import axios from 'axios';
import { defaultSession } from '@lib/Beccaccino';
import { default as axios, AxiosInstance } from 'axios';
import { defaultSession } from '../Beccaccino';
import { ErrorTransform, ResponseTransform, requestHandler } from './requestHandler';

export type Method =
| 'get'
| 'delete'
| 'head'
| 'options'
| 'post'
| 'put'
| 'patch';

export type EndpointConfig = {
path: string,
method: Method,
name: string,
errorTransformer?: ErrorTransform;
responseTransformer?: ResponseTransform,
};

export type BindRequest = {
config: EndpointConfig,
actionName: string,
axiosInstance: AxiosInstance,
signature: Symbol,
};

export type BindedAction = (params: any) => BindedActionPayload;

const bindParamsToURL = (url: string, params: any) => compilePath(url)(params);

export default class Endpoint {
export class Endpoint {
static bindAction(bindRequest: BindRequest): BindedAction {
return ({
urlParams = {},
Expand Down
10 changes: 5 additions & 5 deletions src/endpoint/__tests__/Endpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Endpoint } from '@lib/endpoint';
import {
REDUX_HTTP_CLIENT_REQUEST,
REDUX_HTTP_ACTION_SIGNATURE,
} from '@lib/redux-http';
import axios from 'axios';
import requestHandler from '@lib/endpoint/requestHandler';
import { defaultSession } from '@lib/Beccaccino';
import { Endpoint } from '../';
import { defaultSession } from '../../Beccaccino';
import { requestHandler } from '../requestHandler';

jest.mock(
'@lib/endpoint/requestHandler',
'../requestHandler',
() => {
return {
default: jest.fn(() => ({})),
requestHandler: jest.fn(() => ({})),
};
},
);
Expand Down
10 changes: 5 additions & 5 deletions src/endpoint/__tests__/requestHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import requestHandler from '@lib/endpoint/requestHandler';
import { requestHandler } from '../requestHandler';

const axiosInstance = axios.create();

Expand Down Expand Up @@ -73,13 +73,13 @@ describe('requestHandler', () => {
expect(responseTransformer).toHaveBeenCalledTimes(1);
expect(responseTransformer).toHaveBeenCalledWith('asd');
}).catch(errors => errors);
})
});
});
describe('fails', () => {
it('with rawResponse and data', () => {
axiosInstance.request = jest.fn()
.mockImplementation((conf) => Promise.reject({
response: { data: 'error' }
response: { data: 'error' },
}));
const res = requestHandler({
requestConfiguration: {
Expand All @@ -99,7 +99,7 @@ describe('requestHandler', () => {
});
it('and calls errorTransformer', () => {
const stubbedPromise = Promise.reject({
response: { data: 'error' }
response: { data: 'error' },
});
const errorTransformer = jest.fn();
axiosInstance.request = jest.fn()
Expand All @@ -119,6 +119,6 @@ describe('requestHandler', () => {
'error',
);
});
})
});
});
});
41 changes: 4 additions & 37 deletions src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { AxiosResponse, AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';
import { BindedActionPayload } from '@lib/redux-http';
export type ResponseTransform = (input: AxiosResponse) => any;
export type ErrorTransform = (input: AxiosError) => any;
import { AxiosResponse, AxiosInstance, AxiosRequestConfig } from 'axios';
import { ErrorTransform, ResponseTransform } from './requestHandler';

export type requestHandler = (
requestConfiguration: AxiosRequestConfig,
Expand All @@ -10,36 +8,5 @@ export type requestHandler = (
axiosInstance: AxiosInstance,
) => Promise<AxiosResponse>;

export type Method =
| 'get'
| 'delete'
| 'head'
| 'options'
| 'post'
| 'put'
| 'patch';

export type EndpointConfig = {
path: string,
method: Method,
name: string,
errorTransformer?: ErrorTransform;
responseTransformer?: ResponseTransform,
};

export type BindRequest = {
config: EndpointConfig,
actionName: string,
axiosInstance: AxiosInstance,
signature: Symbol,
};

export type EndpointResponse = {
rawResponse: AxiosResponse | AxiosError,
data: any,
success: boolean,
};

export type BindedAction = (params: any) => BindedActionPayload;

export { default as Endpoint } from '@lib/endpoint/Endpoint';
export * from './Endpoint';
export * from './requestHandler';
15 changes: 12 additions & 3 deletions src/endpoint/requestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { AxiosRequestConfig, AxiosInstance } from 'axios';
import { ErrorTransform, ResponseTransform, EndpointResponse } from '@lib/endpoint';
import { AxiosRequestConfig, AxiosInstance, AxiosResponse, AxiosError } from 'axios';

export type EndpointResponse = {
rawResponse: AxiosResponse | AxiosError,
data: any,
success: boolean,
};

export type ResponseTransform = (input: AxiosResponse) => any;
export type ErrorTransform = (input: AxiosError) => any;

const defaulTransformFunction = (x: any): any => x;
const defaultErrorTransformFunction = (x: any): any => x;
Expand All @@ -10,7 +18,8 @@ type RequestHandlerParams = {
responseTransformer?: ResponseTransform,
axiosInstance: AxiosInstance,
};
export default function requestHandler({

export function requestHandler({
requestConfiguration,
errorTransformer = defaultErrorTransformFunction,
axiosInstance,
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from '@lib/redux-http';
import { default as InternalBeccaccino } from '@lib/Beccaccino';
export * from './redux-http';
export * from './endpoint';
import { beccaccino } from './Beccaccino';

export const Beccaccino = {
configure: InternalBeccaccino.configure,
getClient: InternalBeccaccino.getClient,
configure: beccaccino.configure,
getClient: beccaccino.getClient,
};
5 changes: 1 addition & 4 deletions src/redux-http/__tests__/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import {
REDUX_HTTP_CLIENT_REQUEST,
REDUX_HTTP_CLIENT_RESPONSE,
REDUX_HTTP_CLIENT_ERROR,
} from '@lib/redux-http';
import Beccaccino from '@lib/Beccaccino';

Beccaccino.configure({}, []);
} from '../';

const nextSpyMock = jest.fn();

Expand Down
4 changes: 2 additions & 2 deletions src/redux-http/__tests__/reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
REDUX_HTTP_CLIENT_ERROR,
REDUX_HTTP_CLIENT_REQUEST,
beccaccinoReducer,
} from '@lib/redux-http';
import { defaultSession } from '@lib/Beccaccino';
} from '../';
import { defaultSession } from '../../Beccaccino';

const initialState = {
results: {},
Expand Down
6 changes: 2 additions & 4 deletions src/redux-http/__tests__/selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import {
errorSelector,
loadingSelector,
cancelTokenSelector,
} from '@lib/redux-http';
import Beccaccino, { defaultSession } from '@lib/Beccaccino';

Beccaccino.configure({}, []);
} from '../';
import { defaultSession } from '../../Beccaccino';

const baseState = {
[BECCACCINO_REDUCER_NAME]: {
Expand Down
27 changes: 27 additions & 0 deletions src/redux-http/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const REDUX_HTTP_CLIENT_REQUEST = '@@beccaccino/request';
export const REDUX_HTTP_CLIENT_ERROR = '@@beccaccino/error';
export const REDUX_HTTP_CLIENT_RESPONSE = '@@beccaccino/response';
export const REDUX_HTTP_ACTION_SIGNATURE = Symbol('REDUX:BECCACCINO_ACTION');
import { EndpointResponse } from '../endpoint';

export type BindedRequestDetails = {
urlParams: any,
requestPayload: any,
endpointName: string,
requestId: string,
cancelRequest: Function,
sessionId?: string,
};

export type BindedActionPayload = {
type: string,
signature: Symbol,
requestDetails: BindedRequestDetails,
execAsync: Promise<EndpointResponse>,
};

export type BindedActionResultPayload = {
type: string,
requestDetails: BindedRequestDetails,
response: EndpointResponse,
};
Loading