UNPKG

@basetime/a2w-api-ts

Version:

Client library that communicates with the addtowallet API.

87 lines (86 loc) 2.5 kB
import { Logger } from './Logger'; import CampaignsEndpoint from './endpoint/CampaignsEndpoint'; import ClaimsEndpoint from './endpoint/ClaimsEndpoint'; import OrganizationsEndpoint from './endpoint/OrganizationsEndpoint'; import TemplatesEndpoint from './endpoint/TemplatesEndpoint'; import { AuthProvider } from './provider/AuthProvider'; import { Requester } from './types/Requester'; /** * Client class that communicates with the the addtowallet API. */ export default class Client implements Requester { /** * The authentication object. */ auth?: AuthProvider; /** * The logger. */ protected logger: Logger; /** * The campaigns endpoint. */ protected _campaigns?: CampaignsEndpoint; /** * The claims endpoint. */ protected _claims?: ClaimsEndpoint; /** * The templates endpoint. */ protected _templates?: TemplatesEndpoint; /** * The organizations endpoint. */ protected _organizations?: OrganizationsEndpoint; /** * Constructor. * * @param auth The authentication provider. * @param logger The logger to use. */ constructor(auth?: AuthProvider, logger?: Logger); /** * Sets the base URL for all requests to the API. * * @param url The base URL for all requests to the API. */ setBaseUrl: (url: string) => void; /** * Sets the auth provider to use. * * @param auth The auth provider to use. */ setAuth: (auth: AuthProvider) => void; /** * Returns the campaigns endpoint. * * @returns {CampaignsEndpoint} The campaigns endoint. */ get campaigns(): CampaignsEndpoint; /** * Returns the claims endpoint. * * @returns {ClaimsEndpoint} The claims endpoint. */ get claims(): ClaimsEndpoint; /** * Returns the templates endpoint. */ get templates(): TemplatesEndpoint; /** * Returns the organizations endpoint. */ get organizations(): OrganizationsEndpoint; /** * Sends a request using the fetcher and returns the response. * * Adds the bearer token to the headers and catches errors. * * @param url The url to send the request to. * @param options The fetch options. * @param authenticate Whether to authenticate the request. * @returns The response from the endpoint. */ fetch: <T>(url: string, options?: RequestInit, authenticate?: boolean) => Promise<T>; }