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

Return error tuples for more reliable responses #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"prettier-check": "./scripts/prettier-check",
"prettier": "prettier --write \"{src,tests,types,examples,docs}/**/*.{ts,tsx,js,jsx}\"",
"tests": "jest",
"test": "npm run lint && npm run tests -- --runInBand --coverage",
"typecheck": "tsc --project tsconfig.json --noEmit",
"test": "npm run typecheck && npm run lint && npm run tests -- --runInBand --coverage",
"prepublishOnly": "npm test && npm run dist"
},
"repository": {
Expand Down
9 changes: 5 additions & 4 deletions src/ts/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Dispatch } from 'redux';
import {
AsyncActionSet,
Dict,
ErrorOrResponse,
ExtraMeta,
Options,
RequestParams,
Expand Down Expand Up @@ -53,7 +54,7 @@ export function requestWithConfig(
options: Options = {},
extraMeta: Partial<ExtraMeta> = {}
) {
return (dispatch: Dispatch<any>) => {
return (dispatch: Dispatch<any>): Promise<ErrorOrResponse> => {
const meta = serializeMeta(extraMeta, options);

dispatch({ type: actionSet.REQUEST, meta });
Expand All @@ -67,7 +68,7 @@ export function requestWithConfig(
meta,
});
dispatch(setRequestState(actionSet, 'SUCCESS', response, meta.tag));
return response;
return Promise.resolve([null, response] as ErrorOrResponse);
},
(error: AxiosError) => {
const { shouldRethrow } = options;
Expand All @@ -81,10 +82,10 @@ export function requestWithConfig(
dispatch(setRequestState(actionSet, 'FAILURE', error, meta.tag));

if (shouldRethrow && shouldRethrow(error)) {
return Promise.reject(error);
return Promise.reject([error, null] as ErrorOrResponse);
}

return Promise.resolve();
return Promise.resolve([error, null] as ErrorOrResponse);
}
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ export interface RequestParams extends Options {
readonly metaData?: ExtraMeta;
readonly headers?: Dict<string>;
}

export type ErrorOrResponse = [(AxiosError | null), (AxiosResponse | null)];
11 changes: 8 additions & 3 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ describe('Requests', () => {
it('should return a thunk for sending a generic request', () => {
expect(typeof thunk).toBe('function');
});

it('should dispatch request actions', () => {
myRequest = (thunk(dispatch) as any) as AxiosMock; // FIXME: We need type-safe mocking

Expand Down Expand Up @@ -198,7 +199,7 @@ describe('Requests', () => {

it('should dispatch success actions', () => {
myRequest = (thunk(dispatch) as any) as AxiosMock;
myRequest.success({
const result = myRequest.success({
data: 'llama',
});

Expand All @@ -213,6 +214,10 @@ describe('Requests', () => {
expect(dispatch).toHaveBeenCalledWith(
setRequestState(ACTION_SET, 'SUCCESS', { data: 'llama' }, undefined)
);

return result.then((data: any) => {
expect(data).toEqual([null, { data: 'llama' }]);
});
});

it('should dispatch failure actions', () => {
Expand Down Expand Up @@ -245,7 +250,7 @@ describe('Requests', () => {
)
);
return result.then((data: any) => {
expect(data).toBeUndefined();
expect(data).toEqual([{ response: { data: 'llama' } }, null]);
});
});

Expand All @@ -264,7 +269,7 @@ describe('Requests', () => {
},
})
.catch((error: any) => {
expect(error).toEqual({ response: { data: 'llama' } });
expect(error).toEqual([{ response: { data: 'llama' } }, null]);
});
});
});
Expand Down