UNPKG

3.02 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12const axios = require('./httpclient');
13const config = require('./config');
14
15/**
16 * The Fastly Auth API.
17 *
18 * @see https://docs.fastly.com/api/auth#top
19 * @type {AuthAPI}
20 */
21class AuthAPI {
22 constructor(base) {
23 this.base = base;
24 this.defaultOptions = {
25 baseURL: config.mainEntryPoint,
26 timeout: 15000,
27 headers: {},
28 };
29 }
30
31 get request() {
32 return this.base.request;
33 }
34
35 /**
36 * List all tokens of a customer.
37 *
38 * @see https://docs.fastly.com/api/auth#tokens_d59ff8612bae27a2317278abb048db0c
39 * @param {string} [customerId] - The id of the customer.
40 * @returns {Promise} The response object representing the completion or failure.
41 */
42 async readTokens(customerId = '') {
43 if (customerId) {
44 return this.request.get(`/customer/${customerId}/tokens`);
45 }
46 return this.request.get('/tokens');
47 }
48
49 /**
50 * Get the token with the specified id. If the Id is missing, the self token is returned.
51 *
52 * @see https://docs.fastly.com/api/auth#tokens_bb00e7ed542cbcd7f32b5c908b8ce244
53 * @param {string} [id] - The token id.
54 * @returns {Promise} The response object representing the completion or failure.
55 */
56 async readToken(id) {
57 if (!id) {
58 return this.request.get('/tokens/self');
59 }
60 const ret = await this.request.get('/tokens');
61 const filtered = ret.data.filter((token) => token.id === id);
62 if (filtered.length > 0) {
63 [ret.data] = filtered;
64 return ret;
65 }
66 throw Error('No such token.');
67 }
68
69 /**
70 * Delete the token with the specified id.
71 *
72 * @see https://docs.fastly.com/api/auth#tokens_4a958ba69402500937f0d8570f7ce86f
73 * @param {string} [id] - The token id.
74 * @returns {Promise} The response object representing the completion or failure.
75 */
76 async deleteToken(id) {
77 return this.request.delete(`/tokens/${id}`);
78 }
79
80 /**
81 * Create an API token.
82 *
83 * @see https://docs.fastly.com/api/auth#tokens_db4655a45a0107448eb0676577446e40
84 * @param {object} options - The token options.
85 * @returns {Promise} The response object representing the completion or failure.
86 */
87 async createToken(options) {
88 // send POST w/o authentication.
89 const rp = axios.create({ ...this.defaultOptions });
90 return rp.post('/tokens', options, {
91 headers: {
92 'content-type': 'application/json',
93 },
94 });
95 }
96}
97
98module.exports = AuthAPI;