Skip to content

Commit

Permalink
authenticator: Move library code to this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
esdrasjnr committed Jul 12, 2020
1 parent a98661f commit 6ba707c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"rollup-plugin-node-resolve": "^5.2.0"
},
"dependencies": {
"@cesarbr/knot-cloud-sdk-js-authenticator": "^2.1.0",
"amqp-connection-manager": "^3.2.0",
"amqplib": "^0.5.6",
"axios": "^0.19.2",
Expand Down
28 changes: 26 additions & 2 deletions src/Authenticator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
import Authenticator from '@cesarbr/knot-cloud-sdk-js-authenticator';
import HTTP from './network/HTTP';

export default Authenticator;
export default class Client {
constructor(config = {}) {
this.http = new HTTP({
protocol: 'https',
hostname: 'api.knot.cloud',
...config,
});
}

async createUser(email, password) {
const body = { email, password };
return this.http.post('/users', body);
}

async createToken(email, credential, type, duration) {
const body = {
email,
password: type !== 'app' ? credential : '',
token: type === 'app' ? credential : '',
type,
duration,
};
return this.http.post('/tokens', body);
}
}
24 changes: 20 additions & 4 deletions src/network/HTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ export default class HTTP {
}

this.baseUrl = `${protocol}://${hostname}:${port}${pathname}`;
this.header = {
auth_token: token,
};
this.authToken = token;
}

async get(path, params) {
const config = {
url: `${this.baseUrl}${path}`,
method: 'GET',
headers: this.header,
headers: {
auth_token: this.authToken,
},
params,
};

Expand All @@ -33,6 +33,22 @@ export default class HTTP {
.catch(err => this.handleError(err));
}

async post(path, data) {
const config = {
url: `${this.baseUrl}${path}`,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
data,
};

return axios(config)
.then(res => res.data || JSON.parse(res.config.data))
.catch(err => this.handleError(err));
}

async handleError(r) {
/*
* Reject with a detailed error if the response was correctly received.
Expand Down
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -856,13 +856,6 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@cesarbr/knot-cloud-sdk-js-authenticator@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@cesarbr/knot-cloud-sdk-js-authenticator/-/knot-cloud-sdk-js-authenticator-2.1.0.tgz#f03e0b7957b2ac69bad238f390a0db3b5211c742"
integrity sha512-zvZSqq6+gBKZXLI3LLzd3Qji6iq8JzAnxNW+2HiyFVQ9tJI5emvPLuhkNpsQZCwrJTfC1UMw3h8ixAqpc/M2ow==
dependencies:
axios "^0.19.0"

"@cnakazawa/watch@^1.0.3":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.4.tgz#f864ae85004d0fcab6f50be9141c4da368d1656a"
Expand Down Expand Up @@ -1470,7 +1463,7 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.10.0.tgz#a17b3a8ea811060e74d47d306122400ad4497ae2"
integrity sha512-3YDiu347mtVtjpyV3u5kVqQLP242c06zwDOgpeRnybmXlYYsLbtTrUBUm8i8srONt+FWobl5aibnU1030PeeuA==

axios@^0.19.0, axios@^0.19.2:
axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
Expand Down

0 comments on commit 6ba707c

Please sign in to comment.