2019-02-01 16:19:30 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// 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: []
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response: any = await apollo.query({
|
|
|
|
query: gql(
|
|
|
|
`query {
|
|
|
|
project(
|
|
|
|
id: "${projectID}",
|
|
|
|
) {
|
|
|
|
apiKeys {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
|
|
|
result.data = response.data.project.apiKeys;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
result.errorMessage = e.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createAPIKey(projectID: string, name: string) {
|
|
|
|
let result: RequestResponse<any> = {
|
|
|
|
errorMessage: '',
|
|
|
|
isSuccess: false,
|
|
|
|
data: null
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response: any = await apollo.mutate({
|
|
|
|
mutation: gql(
|
|
|
|
`mutation {
|
|
|
|
createAPIKey(
|
|
|
|
projectID: "${projectID}",
|
|
|
|
name: "${name}"
|
|
|
|
) {
|
|
|
|
key,
|
|
|
|
keyInfo {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache'
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
result.errorMessage = e.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response: any = await apollo.mutate({
|
|
|
|
mutation: gql(
|
|
|
|
`mutation {
|
2019-02-13 11:34:40 +00:00
|
|
|
deleteAPIKeys(id: [${prepareIdList(ids)}]) {
|
2019-02-01 16:19:30 +00:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
),
|
|
|
|
fetchPolicy: 'no-cache'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
result.errorMessage = response.errors[0].message;
|
|
|
|
} else {
|
|
|
|
result.isSuccess = true;
|
2019-02-13 11:34:40 +00:00
|
|
|
result.data = response.data.deleteAPIKeys;
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
result.errorMessage = e.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2019-02-13 11:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepareIdList(ids: string[]): string {
|
|
|
|
let idString: string = '';
|
|
|
|
|
|
|
|
ids.forEach(id => {
|
|
|
|
idString += `"${id}", `;
|
|
|
|
});
|
|
|
|
|
|
|
|
return idString;
|
|
|
|
}
|