Skip to content

Commit

Permalink
Merge pull request #2432 from ChristianMurphy/refactor/move-transform…
Browse files Browse the repository at this point in the history
…er-out-of-axios

refactor: move transformer out of and after validation
  • Loading branch information
ChristianMurphy committed Nov 20, 2020
2 parents 28420ae + 77c3d8d commit f9585ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
9 changes: 3 additions & 6 deletions oeq-ts-rest-api/src/AxiosInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ const catchHandler = (error: AxiosError | Error): never => {
export const GET = <T>(
path: string,
validator: (data: unknown) => data is T,
queryParams?: Parameters<typeof stringify>[0], // eslint-disable-line @typescript-eslint/ban-types
transformer?: (data: unknown) => T
queryParams?: Parameters<typeof stringify>[0] // eslint-disable-line @typescript-eslint/ban-types
): Promise<T> =>
axios
.get(path, {
params: queryParams,
paramsSerializer: (params) => stringify(params),
})
.then(({ data: rawData }: AxiosResponse<unknown>) => {
const data = transformer ? transformer(rawData) : rawData;
.then(({ data }: AxiosResponse<unknown>) => {
if (!validator(data)) {
throw new TypeError(
`Data format mismatch with data received from server, on request to: "${path}"`
Expand Down Expand Up @@ -82,8 +80,7 @@ export const POST = <T, R>(
): Promise<R> =>
axios
.post(path, data)
.then((response: AxiosResponse<unknown>) => {
const data = response.data;
.then(({ data }: AxiosResponse<unknown>) => {
if (!validator(data)) {
throw new TypeError(
`Data format mismatch with data received from server, on request to: "${path}"`
Expand Down
58 changes: 39 additions & 19 deletions oeq-ts-rest-api/src/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ export interface Attachment {
}

/**
* Type of search result item.
* Shared properties or raw and transformed search result item
*/
export interface SearchResultItem {
interface SearchResultItemBase {
/**
* Item's unique ID.
*/
Expand All @@ -193,14 +193,6 @@ export interface SearchResultItem {
* Item's status
*/
status: string;
/**
* The date when item is created.
*/
createdDate: Date;
/**
* The last date when item is modified.
*/
modifiedDate: Date;
/**
* The ID of item's collection.
*/
Expand Down Expand Up @@ -248,6 +240,34 @@ export interface SearchResultItem {
};
}

/**
* Search result item as it is returned by the API
*/
interface SearchResultItemRaw extends SearchResultItemBase {
/**
* The date when item is created.
*/
createdDate: string;
/**
* The last date when item is modified.
*/
modifiedDate: string;
}

/**
* Type of search result item.
*/
export interface SearchResultItem extends SearchResultItemBase {
/**
* The date when item is created.
*/
createdDate: Date;
/**
* The last date when item is modified.
*/
modifiedDate: Date;
}

/**
* Represents the results for a search query.
*/
Expand Down Expand Up @@ -286,15 +306,15 @@ export const search = (
apiBasePath: string,
params?: SearchParams
): Promise<SearchResult<SearchResultItem>> => {
return GET<SearchResult<SearchResultItem>>(
return GET<SearchResult<SearchResultItemRaw>>(
apiBasePath + SEARCH2_API_PATH,
(data): data is SearchResult<SearchResultItem> =>
is<SearchResult<SearchResultItem>>(data),
params,
(data) =>
Utils.convertDateFields<SearchResult<SearchResultItem>>(data, [
'createdDate',
'modifiedDate',
])
(data): data is SearchResult<SearchResultItemRaw> =>
is<SearchResult<SearchResultItemRaw>>(data),
params
).then((data) =>
Utils.convertDateFields<SearchResult<SearchResultItem>>(data, [
'createdDate',
'modifiedDate',
])
);
};

0 comments on commit f9585ef

Please sign in to comment.