Skip to content

Commit

Permalink
Merge pull request #4 from jvoliveiraGN/main
Browse files Browse the repository at this point in the history
Release 1.0.5: Correção do tratamento das requests
  • Loading branch information
jvoliveiraGN authored May 19, 2021
2 parents d4b6411 + 55ff8ca commit cb57682
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 52 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gn-api-sdk-typescript",
"version": "1.0.4",
"version": "1.0.5",
"description": "Module for integration with Gerencianet API\"",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -9,11 +9,14 @@
"prepare": "npm run build",
"postversion": "git push --tags"
},
"author": "Gerencianet - Consultoria Tecnica | João Vitor Oliveira",
"author": "Gerencianet - Consultoria Tecnica | Palloma Brito | João Vitor Oliveira",
"license": "MIT",
"repository": "gerencianet/gn-api-sdk-typescript",
"homepage": "https://github.com/gerencianet/gn-api-sdk-typescript",
"files": ["dist/*", "examples"],
"files": [
"dist/*",
"examples"
],
"keywords": [
"gerencianet",
"pagamentos",
Expand Down
2 changes: 1 addition & 1 deletion src/gn-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class GnAuth {
return res.data;
})
.catch((error) => {
console.error(error);
return error;
});

return response;
Expand Down
70 changes: 22 additions & 48 deletions src/gn-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,80 +57,53 @@ class GnEndpoints {
this.body = body;
this.params = [params];

if (!this.accessToken) {
this.getAccessToken().then(this.directReq.bind(this));
} else {
this.withTokenReq.call(this);
}

this.getAccessToken().then(this.directReq.bind(this));
return this.defer.promise;
}

private getAccessToken(): Promise<any> {
const self = this;
const gnAuth = new GnAuth(this.options, this.constants);
return gnAuth
.getAccessToken()
.then((response) => {
this.accessToken = response.access_token;
return this.accessToken;
self.accessToken = response.access_token;
return response.access_token;
})
.catch((err) => {
return err;
});
}

private getResponse(response: any, body: any) {
return this.options.rawResponse ? response : body;
}

private req() {
private async req(callback: any) {
const req: any = this.getParams.call(this, this.endpoint.route);
req.method = this.endpoint.method;
axios(req)
.then((res) => {
console.log(res.data);
callback(res);
})
.catch((error) => {
console.log(error.response.data);
callback(error);
});
}

private directReq() {
this.directReqCallback.bind(this);
this.req();
this.req(this.directReqCallback.bind(this));
}

private directReqCallback(err: any, httpResponse: { statusCode: number }, bodyResponse: any) {
const response = this.getResponse(httpResponse, bodyResponse);

if (err) {
this.defer.reject(err);
} else if (httpResponse.statusCode !== 200) {
this.defer.reject(response);
} else {
this.defer.resolve(response);
}
}

private withTokenReq() {
this.withTokenReqCallback.bind(this);
}

private withTokenReqCallback(
err: any,
httpResponse: { statusCode: number },
httpResponseBody: any
) {
const response = this.getResponse(httpResponse, httpResponseBody);

if (err) {
this.defer.reject(err);
} else if (httpResponse.statusCode === 401) {
this.getAccessToken().then(this.directReq.bind(this));
} else if (httpResponse.statusCode !== 200) {
this.defer.reject(response);
} else {
this.defer.resolve(response);
private directReqCallback(rawResponse: any) {
if (rawResponse.data) {
if (rawResponse.status < 300) {
if (rawResponse.data.data) {
this.defer.resolve(rawResponse.data.data);
} else {
this.defer.resolve(rawResponse.data);
}
} else {
this.defer.reject(rawResponse.data);
}
} else if (rawResponse.response && rawResponse.response.data) {
this.defer.reject(rawResponse.response.data);
}
}

Expand All @@ -145,6 +118,7 @@ class GnEndpoints {
this.params.forEach((obj: any) => {
if (obj) {
Object.entries(obj).forEach((entrie: any) => {
// eslint-disable-next-line prefer-destructuring
params[entrie[0]] = entrie[1];
});
}
Expand Down

0 comments on commit cb57682

Please sign in to comment.