limits.ts
1 import type { Error as ServerError } from "~/definitions/error"; 2 import type { Limit } from "~/definitions/limit"; 3 import { defaultFetcher, type Fetcher, type Request } from "@literate.ink/utilities"; 4 5 import { CLIENT_TYPE, createRouteREST, SERVICE_VERSION } from "~/core/constants"; 6 import { type Identification, ReauthenticateError } from "~/models"; 7 export type { Limit }; 8 9 // eslint-disable-next-line ts/explicit-function-return-type 10 export const limits = async (identification: Identification, fetcher: Fetcher = defaultFetcher) => { 11 const request: Request = { 12 headers: { 13 Authorization: `Bearer ${identification.accessToken}`, 14 channel: "AIZ", 15 clientVersion: SERVICE_VERSION, 16 format: "T", 17 language: "fr", 18 model: "A", 19 sessionId: identification.sessionID, 20 smoneyClientType: CLIENT_TYPE, 21 userId: identification.identifier, 22 version: "2.0" 23 }, 24 url: createRouteREST("GetCurrentLimits") 25 }; 26 27 const response = await fetcher(request); 28 const json = JSON.parse(response.content) as ServerError | { 29 CurrentLimits: Array<Limit>; 30 CurrentRole: number; 31 ExtendedLimits: Array<Limit>; 32 ExtendedRole: number; 33 KycStatus: number; 34 }; 35 36 if ("ErrorMessage" in json) { 37 if (json.Code === 140 || json.Code === 570) 38 throw new ReauthenticateError(); 39 40 throw new Error(`${json.ErrorMessage} (${json.Code})`); 41 } 42 43 return json; 44 };