|
1 | 1 | /** |
2 | | - * This file auto-generated by Fern from our API Definition. |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import * as environments from "./environments"; |
6 | 6 | import * as core from "./core"; |
7 | | -import { Client as DeviceClient } from "./resources/device/client/Client"; |
8 | | -import { Client as EventClient } from "./resources/event/client/Client"; |
9 | | -import { Client as UserClient } from "./resources/user/client/Client"; |
10 | | -import { RavenApi } from "."; |
| 7 | +import { RavenApi } from "@ravenapp/raven"; |
| 8 | +import urlJoin from "url-join"; |
| 9 | +import * as serializers from "./serialization"; |
| 10 | +import * as errors from "./errors"; |
| 11 | +import { Client as DeviceClient } from "./api/resources/device/client/Client"; |
| 12 | +import { Client as UserClient } from "./api/resources/user/client/Client"; |
11 | 13 |
|
12 | 14 | export declare namespace RavenApiClient { |
13 | | - interface Options { |
14 | | - environment?: environments.Environment | string; |
15 | | - auth?: { |
16 | | - authorization?: core.Supplier<string>; |
17 | | - }; |
18 | | - } |
| 15 | + interface Options { |
| 16 | + environment?: environments.RavenApiEnvironment | string; |
| 17 | + authorization?: core.Supplier<string>; |
| 18 | + } |
19 | 19 | } |
20 | 20 |
|
21 | 21 | export class RavenApiClient { |
22 | | - constructor(private readonly options: RavenApiClient.Options) {} |
| 22 | + constructor(private readonly options: RavenApiClient.Options) {} |
23 | 23 |
|
24 | | - #device: DeviceClient | undefined; |
| 24 | + /** |
| 25 | + * This endpoint allows you to send messages |
| 26 | + * @throws {RavenApi.EventNotFoundError} |
| 27 | + */ |
| 28 | + public async send(appId: RavenApi.AppId, request: RavenApi.SendEventRequest): Promise<RavenApi.SendEventResponse> { |
| 29 | + const { idempotencyKey, ..._body } = request; |
| 30 | + const _response = await core.fetcher({ |
| 31 | + url: urlJoin( |
| 32 | + this.options.environment ?? environments.RavenApiEnvironment.Prod, |
| 33 | + `/v1/apps/${appId}/events/send` |
| 34 | + ), |
| 35 | + method: "POST", |
| 36 | + headers: { |
| 37 | + "Idempotency-Key": idempotencyKey, |
| 38 | + Authorization: await core.Supplier.get(this.options.authorization), |
| 39 | + }, |
| 40 | + body: await serializers.SendEventRequest.json(_body), |
| 41 | + }); |
| 42 | + if (_response.ok) { |
| 43 | + return await serializers.SendEventResponse.parse(_response.body as serializers.SendEventResponse.Raw); |
| 44 | + } |
25 | 45 |
|
26 | | - public get device(): DeviceClient { |
27 | | - return (this.#device ??= new DeviceClient(this.options)); |
28 | | - } |
| 46 | + if (_response.error.reason === "status-code") { |
| 47 | + switch (_response.error.statusCode) { |
| 48 | + case 404: |
| 49 | + throw new RavenApi.EventNotFoundError( |
| 50 | + await serializers.EventNotFoundErrorBody.parse( |
| 51 | + _response.error.body as serializers.EventNotFoundErrorBody.Raw |
| 52 | + ) |
| 53 | + ); |
| 54 | + default: |
| 55 | + throw new errors.RavenApiError({ |
| 56 | + statusCode: _response.error.statusCode, |
| 57 | + body: _response.error.body, |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
29 | 61 |
|
30 | | - #event: EventClient | undefined; |
| 62 | + switch (_response.error.reason) { |
| 63 | + case "non-json": |
| 64 | + throw new errors.RavenApiError({ |
| 65 | + statusCode: _response.error.statusCode, |
| 66 | + body: _response.error.rawBody, |
| 67 | + }); |
| 68 | + case "timeout": |
| 69 | + throw new errors.RavenApiTimeoutError(); |
| 70 | + case "unknown": |
| 71 | + throw new errors.RavenApiError({ |
| 72 | + message: _response.error.errorMessage, |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
31 | 76 |
|
32 | | - public get event(): EventClient { |
33 | | - return (this.#event ??= new EventClient(this.options)); |
34 | | - } |
| 77 | + public async sendBulk( |
| 78 | + appId: RavenApi.AppId, |
| 79 | + request: RavenApi.BulkSendEventRequest |
| 80 | + ): Promise<RavenApi.SendEventResponse> { |
| 81 | + const { idempotencyKey, ..._body } = request; |
| 82 | + const _response = await core.fetcher({ |
| 83 | + url: urlJoin( |
| 84 | + this.options.environment ?? environments.RavenApiEnvironment.Prod, |
| 85 | + `/v1/apps/${appId}/events/bulk_send` |
| 86 | + ), |
| 87 | + method: "POST", |
| 88 | + headers: { |
| 89 | + "Idempotency-Key": idempotencyKey, |
| 90 | + Authorization: await core.Supplier.get(this.options.authorization), |
| 91 | + }, |
| 92 | + body: await serializers.BulkSendEventRequest.json(_body), |
| 93 | + }); |
| 94 | + if (_response.ok) { |
| 95 | + return await serializers.SendEventResponse.parse(_response.body as serializers.SendEventResponse.Raw); |
| 96 | + } |
35 | 97 |
|
36 | | - public async send( |
37 | | - request: RavenApi.event.send.Request |
38 | | - ): Promise<RavenApi.event.send.Response> { |
39 | | - return this.event.send(request); |
40 | | - } |
| 98 | + if (_response.error.reason === "status-code") { |
| 99 | + throw new errors.RavenApiError({ |
| 100 | + statusCode: _response.error.statusCode, |
| 101 | + body: _response.error.body, |
| 102 | + }); |
| 103 | + } |
41 | 104 |
|
42 | | - public async sendBulk( |
43 | | - request: RavenApi.event.sendBulk.Request |
44 | | - ): Promise<RavenApi.event.sendBulk.Response> { |
45 | | - return this.event.sendBulk(request); |
46 | | - } |
| 105 | + switch (_response.error.reason) { |
| 106 | + case "non-json": |
| 107 | + throw new errors.RavenApiError({ |
| 108 | + statusCode: _response.error.statusCode, |
| 109 | + body: _response.error.rawBody, |
| 110 | + }); |
| 111 | + case "timeout": |
| 112 | + throw new errors.RavenApiTimeoutError(); |
| 113 | + case "unknown": |
| 114 | + throw new errors.RavenApiError({ |
| 115 | + message: _response.error.errorMessage, |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
47 | 119 |
|
48 | | - #user: UserClient | undefined; |
| 120 | + #device: DeviceClient | undefined; |
49 | 121 |
|
50 | | - public get user(): UserClient { |
51 | | - return (this.#user ??= new UserClient(this.options)); |
52 | | - } |
| 122 | + public get device(): DeviceClient { |
| 123 | + return (this.#device ??= new DeviceClient(this.options)); |
| 124 | + } |
| 125 | + |
| 126 | + #user: UserClient | undefined; |
| 127 | + |
| 128 | + public get user(): UserClient { |
| 129 | + return (this.#user ??= new UserClient(this.options)); |
| 130 | + } |
53 | 131 | } |
0 commit comments