-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: interfaces standardization and ResponseHandlers simplification
- Refactor & move interfaces in separate files - Clear jest cache on npm test command - Remove UnauthorizedResponseHandler - Simplify default response handlers - Test default response handlers - Export default resposne handlers - Rename CoveoPlatform for PlatformClient - Add named export of PlatformClient BREAKING CHANGE: - Rename type `IRestResponseHandler` to `ResponseHandler` - Remove type `IRestError` - Remove type `IRestResponse` - All calls return `Promise<T>` directly instead of `Promise<IRestResponse<T>>`
- Loading branch information
Showing
23 changed files
with
144 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {ResponseHandler} from './handlers/ResponseHandlerInterfaces'; | ||
|
||
export interface APIConfiguration { | ||
organizationId: string; | ||
accessTokenRetriever: () => string; | ||
host?: string; | ||
responseHandlers?: ResponseHandler[]; | ||
} | ||
|
||
export interface PlatformClientOptions extends APIConfiguration { | ||
environment?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ResponseHandler { | ||
canProcess(response: Response): boolean; | ||
process<T>(response: Response): Promise<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {ResponseHandler} from './ResponseHandlerInterfaces'; | ||
|
||
const noContent: ResponseHandler = { | ||
canProcess: (response: Response): boolean => response.status === 204, | ||
process: async <T>(): Promise<T> => ({} as T), | ||
}; | ||
|
||
const success: ResponseHandler = { | ||
canProcess: (response: Response): boolean => response.ok, | ||
process: async <T>(response: Response): Promise<T> => await response.json(), | ||
}; | ||
|
||
const error: ResponseHandler = { | ||
canProcess: () => true, | ||
process: async <T>(response: Response): Promise<T> => { | ||
throw await response.json(); | ||
}, | ||
}; | ||
|
||
export const defaultResponseHandlers = [noContent, success, error]; | ||
export const ResponseHandlers = {noContent, success, error}; | ||
|
||
export default function<T>(response: Response, handlers = defaultResponseHandlers) { | ||
return handlers.filter((handler) => handler.canProcess(response))[0].process<T>(response); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import handleResponse from '../ResponseHandlers'; | ||
|
||
describe('ResponseHandlers', () => { | ||
it('should return a promise resolved with an empty object when the response status code is 204', async () => { | ||
const noContentResponse = new Response(null, {status: 204}); | ||
const ret = await handleResponse(noContentResponse); | ||
expect(ret).toEqual({}); | ||
}); | ||
|
||
it('should return a promise resolved with the response body when the response status is between 200 and 299', async () => { | ||
const data = {someData: 'thank you!'}; | ||
|
||
const okResponse = new Response(JSON.stringify(data), {status: 200}); | ||
const ret1 = await handleResponse(okResponse); | ||
expect(ret1).toEqual(data); | ||
|
||
const stilOkResponse = new Response(JSON.stringify(data), {status: 299}); | ||
const ret2 = await handleResponse(stilOkResponse); | ||
expect(ret2).toEqual(data); | ||
}); | ||
|
||
it('should return a promise rejected with the response body when the status is not between 200 and 299 ', async () => { | ||
const error = {code: 'WRONG_UTENSIL', message: 'Use a spoon to eat the soup.'}; | ||
const errorResponse = new Response(JSON.stringify(error), {status: 400}); | ||
|
||
try { | ||
await handleResponse(errorResponse); | ||
} catch (err) { | ||
expect(err).toEqual(error); | ||
} | ||
|
||
expect.assertions(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.