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';
|
|
|
|
|
|
|
|
export async function fetchAPIKeys(projectID: string) {
|
|
|
|
let result: RequestResponse<any[]> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: []
|
|
|
|
};
|
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apollo.query({
|
|
|
|
query: gql(
|
|
|
|
`query {
|
|
|
|
project(
|
|
|
|
id: "${projectID}",
|
|
|
|
) {
|
|
|
|
apiKeys {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
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.project.apiKeys;
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createAPIKey(projectID: string, name: string) {
|
|
|
|
let result: RequestResponse<any> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: null
|
|
|
|
};
|
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apollo.mutate({
|
|
|
|
mutation: gql(
|
|
|
|
`mutation {
|
|
|
|
createAPIKey(
|
|
|
|
projectID: "${projectID}",
|
|
|
|
name: "${name}"
|
|
|
|
) {
|
|
|
|
key,
|
|
|
|
keyInfo {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
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 = {
|
|
|
|
key: response.data.createAPIKey.key,
|
|
|
|
keyInfo: response.data.createAPIKey.keyInfo
|
|
|
|
};
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-13 11:34:40 +00:00
|
|
|
export async function deleteAPIKeys(ids: string[]) {
|
2019-02-01 16:19:30 +00:00
|
|
|
let result: RequestResponse<any> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: null
|
|
|
|
};
|
|
|
|
|
2019-04-05 16:08:14 +01:00
|
|
|
let response: any = await apollo.mutate({
|
|
|
|
mutation: gql(
|
|
|
|
`mutation {
|
|
|
|
deleteAPIKeys(id: [${prepareIdList(ids)}]) {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function prepareIdList(ids: string[]): string {
|
2019-02-20 13:33:56 +00:00
|
|
|
let idString: string = '';
|
2019-02-13 11:34:40 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
ids.forEach(id => {
|
|
|
|
idString += `"${id}", `;
|
|
|
|
});
|
2019-02-13 11:34:40 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
return idString;
|
2019-02-13 11:34:40 +00:00
|
|
|
}
|