Skip to content

Commit

Permalink
fix: ajuste de response da api
Browse files Browse the repository at this point in the history
  • Loading branch information
leguass7 committed Nov 14, 2021
1 parent c2806e5 commit a56ca60
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
9 changes: 5 additions & 4 deletions dist/providers/BaseProvider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ class BaseProvider {
}
configureResponses() {
this.Api.interceptors.response.use(response => {
this.log('RESPONSE:', typeof response?.data, response?.data);
if (response?.data && typeof response?.data === 'string') {
return (0, camelcase_keys_1.default)((0, onResponseError_1.convertStringResponseData)(response?.data), { deep: true });
const data = response?.data || response;
this.log(`RESPONSE: (${response?.statusText})`, typeof data, data);
if (data && typeof data === 'string') {
return (0, camelcase_keys_1.default)((0, onResponseError_1.convertStringResponseData)(data), { deep: true });
}
return (0, camelcase_keys_1.default)(response.data, { deep: true });
return (0, camelcase_keys_1.default)(data, { deep: true });
}, onResponseError_1.onResponseError);
return this;
}
Expand Down
4 changes: 4 additions & 0 deletions dist/providers/BaseProvider/onResponseError.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ async function onResponseError(error) {
const data = Object.assign({}, result, convertStringResponseData(response));
return Promise.resolve({ data });
}
if (typeof response?.data === 'string') {
const data = Object.assign({}, result, convertStringResponseData(response?.data));
return Promise.resolve({ data });
}
result.message = `httpError ${statusHttp}`;
return Promise.resolve({ data: result });
}
Expand Down
2 changes: 2 additions & 0 deletions dist/providers/SacDigital/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class SacDigital extends BaseProvider_1.BaseProvider {
return this.buildError('API is not ready');
}
async sendNotification(payload) {
if (!payload?.contact)
return this.buildError('no contact informed');
try {
const res = await this.apiPost(constants_1.ReqType.NOTIFICATION, payload);
return (0, dto_1.responseSendingDto)(res);
Expand Down
9 changes: 5 additions & 4 deletions src/providers/BaseProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ export class BaseProvider {

protected configureResponses() {
this.Api.interceptors.response.use(response => {
this.log('RESPONSE:', typeof response?.data, response?.data);
if (response?.data && typeof response?.data === 'string') {
return camelcaseKeys(convertStringResponseData(response?.data), { deep: true });
const data = response?.data || response;
this.log(`RESPONSE: (${response?.statusText})`, typeof data, data);
if (data && typeof data === 'string') {
return camelcaseKeys(convertStringResponseData(data), { deep: true });
}
return camelcaseKeys(response.data, { deep: true });
return camelcaseKeys(data, { deep: true });
}, onResponseError);
return this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/providers/BaseProvider/onResponseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export async function onResponseError(error?: AxiosError): Promise<{ data: IResu
return Promise.resolve({ data });
}

if (typeof response?.data === 'string') {
const data = Object.assign({}, result, convertStringResponseData(response?.data));
return Promise.resolve({ data });
}

result.message = `httpError ${statusHttp}`;
return Promise.resolve({ data: result });
}
Expand Down
1 change: 1 addition & 0 deletions src/providers/SacDigital/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export class SacDigital extends BaseProvider implements IProvider {
}

private async sendNotification(payload: ISacDigitalRequestSend): Promise<IResponseSending | IResultError> {
if (!payload?.contact) return this.buildError('no contact informed');
try {
const res = await this.apiPost(ReqType.NOTIFICATION, payload);
return responseSendingDto(res);
Expand Down

0 comments on commit a56ca60

Please sign in to comment.