diff --git a/package.json b/package.json index c313e6c..3c7ba24 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Authenticator.js b/src/Authenticator.js index 876c228..0ba661c 100644 --- a/src/Authenticator.js +++ b/src/Authenticator.js @@ -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); + } +} diff --git a/src/network/HTTP.js b/src/network/HTTP.js index 3804d2e..bde6db1 100644 --- a/src/network/HTTP.js +++ b/src/network/HTTP.js @@ -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, }; @@ -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. diff --git a/yarn.lock b/yarn.lock index e8eaed4..2d75779 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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==