export type MessageCreateRequest = {topicId: string, text: string, externalId?: string | undefined}; export type CreateApiV2MessagesArgs = {'X-Signature'?: string | undefined, 'X-Timestamp'?: number | undefined, body: MessageCreateRequest}; export type CreateApiV2MessagesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, hmacSignature?: string | undefined, fetch?: typeof fetch | undefined}; /** * Send a message * * POST /v2/messages * * Also accepts auth: oauth2ClientCredentials */ export const createApiV2Messages = async (args: CreateApiV2MessagesArgs, options?: CreateApiV2MessagesOptions) => { const {baseUrl = "https://api.zenzap.co", fetch: fetchFn = fetch, bearerAuth, hmacSignature} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (args['X-Signature'] != null) headers.set("X-Signature", String(args['X-Signature'])) if (args['X-Timestamp'] != null) headers.set("X-Timestamp", String(args['X-Timestamp'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) if (hmacSignature !== undefined) headers.set("X-Signature", hmacSignature) const url = `${baseUrl.replace(/\/+$/, '')}/v2/messages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };