import req from './../../../fetch'; import { paramsToString, undocumentedResponse } from '../../../utils'; import { AllAPIResponses, CarbonError, CarbonUserTokenType, } from './../../../index'; type Params = { userId: number; }; type Payload = { // The access id of the user token - this access id must be globally unique, and cannot be reused. accessId: string; // This is the field the user can use to add context to an api key. name: string; // The secret part of the key, must meet requirements, cannot be retrieved. secret: string; }; export const post = async ( params: Params, payload: Payload, headers?: Headers ): Promise> => { try { const resp = await req.post( `/api/authentication/usertoken${paramsToString(params)}`, JSON.stringify(payload), headers ); const clone = resp.clone(); switch (resp.status) { case 200: return { data: (await resp.json()) as CarbonUserTokenType, response: clone, }; case 400: case 401: case 403: case 404: case 500: return { error: (await resp.json()) as CarbonError, response: clone, }; default: return { error: new Error(undocumentedResponse(resp)), response: clone, }; } } catch (e) { return { error: e, response: undefined }; } }; export default post;