All files / src/api/account projects.js

100% Statements 10/10
100% Branches 2/2
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                          4x                               3x 2x   2x     1x   1x       1x   1x                     1x     6x          
// @flow
 
import { type Caller } from '.';
 
/**
 * A description of a project.
 */
export type AccountProject = {
  id: number,
  active: boolean,
  created: Date,
  name: string,
  description: string,
  displayName: string,
  projectId: string,
  type?: string,
  subscriptionNumber?: string,
  productCode?: string,
};
 
/**
 * Project API keys.
 */
export type AccountProjectKeys = {
  apiKey: string,
  secretKey: string,
};
 
/**
 * Explore the projects you belong to.
 */
export interface AccountProjectsAPI {
  list(id?: number): Promise<AccountProject | Array<AccountProject>>;
  keys(id: number): Promise<AccountProjectKeys>;
}
 
function into(project: any): AccountProject {
  return {
    id: project.id,
    active: project.active,
    name: project.group_name,
    displayName: project.display_name,
    description: project.description,
    projectId: project.project_id,
    created: new Date(project.date_created),
    type: project.source,
    subscriptionNumber: project.subscription_number,
    productCode: project.product_code,
  };
}
 
export default function projects(call: Caller): AccountProjectsAPI {
  async function list(id) {
    if (id) {
      const project = await call('GET', `user-projects/${id}`);
 
      return into(project);
    }
 
    const { objects } = await call('GET', 'user-projects');
 
    return objects.map(into);
  }
 
  async function keys(id) {
    const { name } = await list(id);
 
    const { api_public_key: apiKey, api_private_key: secretKey } = await call(
      'GET',
      'account-keys/get_project_keys',
      {
        params: {
          groupName: name,
          groupId: id,
        },
      },
    );
 
    return { apiKey, secretKey };
  }
 
  return {
    list,
    keys,
  };
}