diff --git a/src/endpoints/APIBaseWrapper.ts b/src/endpoints/APIBaseWrapper.ts index 92a37c3..90b54a2 100644 --- a/src/endpoints/APIBaseWrapper.ts +++ b/src/endpoints/APIBaseWrapper.ts @@ -39,7 +39,7 @@ export default class APIBaseWrapper { this.onRequest('GET', path); return this.client - .get(path) + .get(path) .then(({data}) => data) .then(handleResponseBody); } @@ -47,19 +47,19 @@ export default class APIBaseWrapper { protected _wrapPost(path: string, params = {}): Promise { this.onRequest('POST', path, params); - return this.client.post(path, params).then(({data}) => data); + return this.client.post(path, params).then(({data}) => data); } protected _wrapPatch(path: string, params = {}): Promise { this.onRequest('PATCH', path, params); - return this.client.patch(path, params).then(({data}) => data); + return this.client.patch(path, params).then(({data}) => data); } protected _wrapDelete(path: string): Promise { this.onRequest('DELETE', path); - return this.client.delete(path).then(({data}) => data); + return this.client.delete(path).then(({data}) => data); } /** diff --git a/src/endpoints/ItemsWrapper.ts b/src/endpoints/ItemsWrapper.ts index 7bb48d8..9798df6 100644 --- a/src/endpoints/ItemsWrapper.ts +++ b/src/endpoints/ItemsWrapper.ts @@ -1,5 +1,5 @@ import APIBaseWrapper from './APIBaseWrapper'; -import type {ItemListPromise, ItemPromise} from '../types'; +import type {Item, ItemListPromise, ItemPromise} from '../types'; export default class ItemsWrapper extends APIBaseWrapper { getAllItems(): ItemListPromise { @@ -8,10 +8,10 @@ export default class ItemsWrapper extends APIBaseWrapper { getItem(itemId: number, detail: boolean = false): ItemPromise { const url = !detail ? `items/${itemId}` : `items/${itemId}?detail=true`; - return this._wrapGet(url); + return this._wrapGet(url); } - getItems(...itemIds: Array): ItemListPromise { + getItems(itemIds: number[]): ItemListPromise { return this._wrapGet(`items/multi/${itemIds.join(',')}`); } diff --git a/src/lib/handlers.ts b/src/lib/handlers.ts index b4e76b2..b232796 100644 --- a/src/lib/handlers.ts +++ b/src/lib/handlers.ts @@ -1,11 +1,21 @@ -export function handleResponseBody(body: any) { +/** + * Unwrap the response body from an AxiosResponse + * + * This function will return the `data` property of the response body if it exists, + * otherwise it will return the body itself. + * + * @param body + */ +export function handleResponseBody(body: MaybeDataWrapped): T | null { if (!body) { return null; } - if (!('data' in body)) { + if (typeof body !== 'object') { return body; } - return body.data; + return 'data' in body ? body.data : body; } + +type MaybeDataWrapped = T | {data: T};