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.
|
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
import { BaseGql } from '@/api/baseGql';
|
2019-09-12 15:10:50 +01:00
|
|
|
import { ApiKey, ApiKeyCursor, ApiKeysApi, ApiKeysPage } from '@/types/apiKeys';
|
2019-08-19 11:00:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ApiKeysApiGql is a graphql implementation of ApiKeys API.
|
|
|
|
* Exposes all apiKey-related functionality
|
|
|
|
*/
|
|
|
|
export class ApiKeysApiGql extends BaseGql implements ApiKeysApi {
|
|
|
|
/**
|
|
|
|
* Fetch apiKeys
|
|
|
|
*
|
|
|
|
* @returns ApiKey
|
|
|
|
* @throws Error
|
|
|
|
*/
|
2019-09-12 15:10:50 +01:00
|
|
|
public async get(projectId: string, cursor: ApiKeyCursor): Promise<ApiKeysPage> {
|
2019-08-19 11:00:38 +01:00
|
|
|
const query =
|
2019-09-12 15:10:50 +01:00
|
|
|
`query($projectId: String!, $limit: Int!, $search: String!, $page: Int!, $order: Int!, $orderDirection: Int!) {
|
|
|
|
project (
|
2019-08-05 16:43:52 +01:00
|
|
|
id: $projectId,
|
2019-04-05 16:08:14 +01:00
|
|
|
) {
|
2019-09-12 15:10:50 +01:00
|
|
|
apiKeys (
|
|
|
|
cursor: {
|
|
|
|
limit: $limit,
|
|
|
|
search: $search,
|
|
|
|
page: $page,
|
|
|
|
order: $order,
|
|
|
|
orderDirection: $orderDirection
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
apiKeys {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
search,
|
|
|
|
limit,
|
|
|
|
order,
|
|
|
|
pageCount,
|
|
|
|
currentPage,
|
|
|
|
totalCount
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
2019-04-05 16:08:14 +01:00
|
|
|
}
|
2019-08-19 11:00:38 +01:00
|
|
|
}`;
|
2019-04-05 16:08:14 +01:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
const variables = {
|
2019-09-12 15:10:50 +01:00
|
|
|
projectId: projectId,
|
|
|
|
limit: cursor.limit,
|
|
|
|
search: cursor.search,
|
|
|
|
page: cursor.page,
|
|
|
|
order: cursor.order,
|
|
|
|
orderDirection: cursor.orderDirection,
|
2019-08-19 11:00:38 +01:00
|
|
|
};
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
const response = await this.query(query, variables);
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
return this.getApiKeysPage(response.data.project.apiKeys);
|
2019-08-19 11:00:38 +01:00
|
|
|
}
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
/**
|
|
|
|
* Used to create apiKey
|
|
|
|
*
|
|
|
|
* @param projectId - stores current project id
|
|
|
|
* @param name - name of apiKey that will be created
|
|
|
|
* @returns ApiKey
|
|
|
|
* @throws Error
|
|
|
|
*/
|
|
|
|
public async create(projectId: string, name: string): Promise<ApiKey> {
|
|
|
|
const query =
|
|
|
|
`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-19 11:00:38 +01:00
|
|
|
}`;
|
|
|
|
|
|
|
|
const variables = {
|
|
|
|
projectId,
|
|
|
|
name,
|
|
|
|
};
|
2019-04-05 16:08:14 +01:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
const response = await this.mutate(query, variables);
|
2019-09-09 11:33:39 +01:00
|
|
|
const key: any = response.data.createAPIKey.keyInfo;
|
|
|
|
const secret: string = response.data.createAPIKey.key;
|
2019-07-18 14:39:39 +01:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
return new ApiKey(key.id, key.name, key.createdAt, secret);
|
2019-02-01 16:19:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
/**
|
|
|
|
* Used to delete apiKey
|
|
|
|
*
|
|
|
|
* @param ids - ids of apiKeys that will be deleted
|
|
|
|
* @throws Error
|
|
|
|
*/
|
2019-09-12 15:10:50 +01:00
|
|
|
public async delete(ids: string[]): Promise<void> {
|
2019-08-19 11:00:38 +01:00
|
|
|
const query =
|
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-19 11:00:38 +01:00
|
|
|
}`;
|
2019-04-05 16:08:14 +01:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
const variables = {
|
|
|
|
id: ids,
|
|
|
|
};
|
2019-02-01 16:19:30 +00:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
const response = await this.mutate(query, variables);
|
2019-02-13 11:34:40 +00:00
|
|
|
|
2019-08-19 11:00:38 +01:00
|
|
|
return response.data.deleteAPIKeys;
|
2019-07-18 14:39:39 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
private getApiKeysPage(page: any): ApiKeysPage {
|
|
|
|
if (!page) {
|
|
|
|
return new ApiKeysPage();
|
2019-08-19 11:00:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:10:50 +01:00
|
|
|
const apiKeysPage: ApiKeysPage = new ApiKeysPage();
|
|
|
|
|
|
|
|
apiKeysPage.apiKeys = page.apiKeys.map(key => new ApiKey(key.id, key.name, key.createdAt, ''));
|
|
|
|
|
|
|
|
apiKeysPage.search = page.search;
|
|
|
|
apiKeysPage.limit = page.limit;
|
|
|
|
apiKeysPage.order = page.order;
|
|
|
|
apiKeysPage.orderDirection = page.orderDirection;
|
|
|
|
apiKeysPage.pageCount = page.pageCount;
|
|
|
|
apiKeysPage.currentPage = page.currentPage;
|
|
|
|
apiKeysPage.totalCount = page.totalCount;
|
|
|
|
|
|
|
|
return apiKeysPage;
|
2019-08-19 11:00:38 +01:00
|
|
|
}
|
2019-07-18 14:39:39 +01:00
|
|
|
}
|