Add base support for creating a new API key for an account

This commit is contained in:
Dane Everitt 2020-03-22 18:15:38 -07:00
parent 32f25170f1
commit 933a4733e8
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
17 changed files with 371 additions and 13 deletions

View file

@ -0,0 +1,17 @@
import http from '@/api/http';
import { ApiKey, rawDataToApiKey } from '@/api/account/getApiKeys';
export default (description: string, allowedIps: string): Promise<ApiKey & { secretToken: string }> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/account/api-keys`, {
description,
// eslint-disable-next-line @typescript-eslint/camelcase
allowed_ips: allowedIps.length > 0 ? allowedIps.split('\n') : [],
})
.then(({ data }) => resolve({
...rawDataToApiKey(data.attributes),
secretToken: data.meta?.secret_token ?? '',
}))
.catch(reject);
});
};

View file

@ -0,0 +1,25 @@
import http from '@/api/http';
export interface ApiKey {
identifier: string;
description: string;
allowedIps: string[];
createdAt: Date | null;
lastUsedAt: Date | null;
}
export const rawDataToApiKey = (data: any): ApiKey => ({
identifier: data.identifier,
description: data.description,
allowedIps: data.allowed_ips,
createdAt: data.created_at ? new Date(data.created_at) : null,
lastUsedAt: data.last_used_at ? new Date(data.last_used_at) : null,
});
export default (): Promise<ApiKey[]> => {
return new Promise((resolve, reject) => {
http.get('/api/client/account/api-keys')
.then(({ data }) => resolve((data.data || []).map(rawDataToApiKey)))
.catch(reject);
});
};