Skip to content

Commit

Permalink
fix: 🐛 removed checking method type
Browse files Browse the repository at this point in the history
  • Loading branch information
chantouchsek committed Jan 26, 2023
1 parent f4fe511 commit 5f87c91
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 55 deletions.
17 changes: 0 additions & 17 deletions src/__tests__/post-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,4 @@ describe('PostService', () => {
expect(data.length).toEqual(1)
expect(item.pagination.page).toEqual(1)
})
it('it should throw exception if method is not inlist', async () => {
const items = {
data: [{ name: 'Chantouch', post_id: 1 }],
meta: {
pagination: { count: 1, page: 1, perPage: 20 },
include: [],
},
}
mockAdapter.onGet('/posts/1/tags').reply(500, items)
try {
await service.throwException(1)
} catch (e: any) {
expect(e.message).toEqual(
'`unlink` is not a valid request type, must be one of: `get`, `GET`, `delete`, `DELETE`, `head`, `HEAD`, `options`, `OPTIONS`, `post`, `POST`, `put`, `PUT`, `patch`, `PATCH`.',
)
}
})
})
40 changes: 3 additions & 37 deletions src/core/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ class BaseService {
const parameter = id && !isObject(id) ? `/${id}` : ''
const body = isObject(id) ? id : payload
const requestType: Method = hasFiles(body) ? 'post' : 'put'
if (hasFiles(body)) {
Object.assign(body, { _method: 'put' })
}
if (hasFiles(body)) Object.assign(body, { _method: 'put' })
return this.submit<T>(requestType, parameter, body, config)
}

Expand Down Expand Up @@ -96,7 +94,6 @@ class BaseService {
}

$submit<T = any>(method: Method, param?: string | number, form?: any, config?: AxiosRequestConfig) {
BaseService.__validateRequestType(method)
this.beforeSubmit()
return new Promise<AxiosResponse<T>>((resolve, reject) => {
const data = hasFiles(form) ? objectToFormData(form) : form
Expand Down Expand Up @@ -124,39 +121,10 @@ class BaseService {
}

private __getParameterString(url: string) {
const query = qs.stringify(this.parameters, {
encode: false,
skipNulls: true,
addQueryPrefix: true,
})
const query = qs.stringify(this.parameters, { encode: false, skipNulls: true, addQueryPrefix: true })
return `${url}${query}`
}

private static __validateRequestType(requestType: Method) {
const requestTypes: Method[] = [
'get',
'GET',
'delete',
'DELETE',
'head',
'HEAD',
'options',
'OPTIONS',
'post',
'POST',
'put',
'PUT',
'patch',
'PATCH',
]
if (!requestTypes.includes(requestType)) {
throw new Error(
`\`${requestType}\` is not a valid request type, ` + `must be one of: \`${requestTypes.join('`, `')}\`.`,
)
}
return requestType
}

setParameters(parameters: Record<string, any>): this {
Object.keys(parameters).forEach((key) => {
this.parameters[key] = parameters[key]
Expand All @@ -182,9 +150,7 @@ class BaseService {
if (!parameters || !parameters.length) {
this.parameters = []
} else if (isArray(parameters)) {
for (const parameter of parameters) {
delete this.parameters[parameter]
}
for (const parameter of parameters) delete this.parameters[parameter]
}
return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/PostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PostService extends BaseService {
super('posts', parameters)
}

tags<T>(id: string | number) {
tags<T = any>(id: string | number) {
return this.submit<T>('get', `${id}/tags`)
}

Expand Down

0 comments on commit 5f87c91

Please sign in to comment.