2019-05-02 14:48:47 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-02-01 16:19:30 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import apollo from '@/utils/apolloManager';
|
|
|
|
import gql from 'graphql-tag';
|
2019-07-18 14:39:39 +01:00
|
|
|
import { ApiKey } from '@/types/apiKeys';
|
|
|
|
import { RequestResponse } from '@/types/response';
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-08-05 16:43:52 +01:00
|
|
|
export async function fetchAPIKeys(projectId: string): Promise<RequestResponse<ApiKey[]>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<ApiKey[]> = new RequestResponse<ApiKey[]>();
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apollo.query({
|
2019-08-05 16:43:52 +01:00
|
|
|
query: gql(`
|
|
|
|
query($projectId: String!) {
|
2019-04-05 16:08:14 +01:00
|
|
|
project(
|
2019-08-05 16:43:52 +01:00
|
|
|
id: $projectId,
|
2019-04-05 16:08:14 +01:00
|
|
|
) {
|
|
|
|
apiKeys {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
2019-08-05 16:43:52 +01:00
|
|
|
variables: {
|
|
|
|
projectId: projectId
|
|
|
|
},
|
2019-04-05 16:08:14 +01:00
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
2019-07-18 14:39:39 +01:00
|
|
|
result.data = getApiKeysList(response.data.project.apiKeys);
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-05 16:43:52 +01:00
|
|
|
export async function createAPIKey(projectId: string, name: string): Promise<RequestResponse<ApiKey>> {
|
2019-07-18 14:39:39 +01:00
|
|
|
let result: RequestResponse<ApiKey> = new RequestResponse<ApiKey>();
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apollo.mutate({
|
2019-08-05 16:43:52 +01:00
|
|
|
mutation: gql(`
|
|
|
|
mutation($projectId: String!, $name: String!) {
|
2019-04-05 16:08:14 +01:00
|
|
|
createAPIKey(
|
2019-08-05 16:43:52 +01:00
|
|
|
projectID: $projectId,
|
|
|
|
name: $name
|
2019-04-05 16:08:14 +01:00
|
|
|
) {
|
|
|
|
key,
|
|
|
|
keyInfo {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
2019-08-05 16:43:52 +01:00
|
|
|
variables: {
|
|
|
|
projectId: projectId,
|
|
|
|
name: name
|
|
|
|
},
|
2019-04-05 16:08:14 +01:00
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
2019-07-18 14:39:39 +01:00
|
|
|
let key: any = response.data.createAPIKey.keyInfo;
|
|
|
|
let secret: string = response.data.createAPIKey.key;
|
|
|
|
|
|
|
|
result.data = new ApiKey(key.id, key.name, key.createdAt, secret);
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:39:39 +01:00
|
|
|
export async function deleteAPIKeys(ids: string[]): Promise<RequestResponse<null>> {
|
|
|
|
// TODO: find needed type instead of any
|
|
|
|
let result: RequestResponse<any> = new RequestResponse<any>();
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apollo.mutate({
|
|
|
|
mutation: gql(
|
2019-08-05 16:43:52 +01:00
|
|
|
`mutation($id: [String!]!) {
|
|
|
|
deleteAPIKeys(id: $id) {
|
2019-04-05 16:08:14 +01:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
2019-08-05 16:43:52 +01:00
|
|
|
variables:{
|
|
|
|
id: ids
|
|
|
|
},
|
2019-04-05 16:08:14 +01:00
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
result.data = response.data.deleteAPIKeys;
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2019-02-13 11:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 14:39:39 +01:00
|
|
|
function getApiKeysList(apiKeys: ApiKey[]): ApiKey[] {
|
|
|
|
if (!apiKeys) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKeys.map(key => new ApiKey(key.id, key.name, key.createdAt, ''));
|
|
|
|
}
|