export type DeviceId = string; export type MqttTopic = string; export type Message = Record; export type Base64EncodedBinaryData = string; export type DeviceMessageInput = {topic?: MqttTopic | undefined, message: Message | Array | Base64EncodedBinaryData}; export type CreateApiDevicesDeviceIdMessagesArgs = {deviceId: DeviceId, body: DeviceMessageInput}; export type CreateApiDevicesDeviceIdMessagesOptions = {baseUrl?: string | undefined, SimpleToken?: string | undefined, fetch?: typeof fetch | undefined}; /** * SendDeviceMessage * * POST /devices/{deviceId}/messages */ export const createApiDevicesDeviceIdMessages = async (args: CreateApiDevicesDeviceIdMessagesArgs, options?: CreateApiDevicesDeviceIdMessagesOptions) => { const {baseUrl = "https://api.nrfcloud.com/v1", fetch: fetchFn = fetch, SimpleToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (SimpleToken !== undefined) headers.set('authorization', `Bearer ${SimpleToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/devices/${encodeURIComponent(String(args.deviceId))}/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 };