|
| 1 | +import { HttpClient, Requester } from "./http.ts"; |
| 2 | +import { Topics } from "./topic.ts"; |
| 3 | +import { Endpoints } from "./endpoint.ts"; |
| 4 | +export type ClientConfig = { |
| 5 | + /** |
| 6 | + * Url of the qstash api server |
| 7 | + * |
| 8 | + * @default "https://qstash.upstash.io" |
| 9 | + */ |
| 10 | + baseUrl?: string; |
| 11 | + |
| 12 | + /** |
| 13 | + * The authorization token from the upstash console. |
| 14 | + */ |
| 15 | + authorization: string; |
| 16 | +}; |
| 17 | + |
| 18 | +type PublishRequest = { |
| 19 | + /** |
| 20 | + * The url of a publicly accessible server where you want to send this message to. |
| 21 | + * The url must have a valid scheme (http or https). |
| 22 | + * |
| 23 | + * Alternatively, you can specify a topic name or id instead of a url to publish to a topic. |
| 24 | + */ |
| 25 | + destination: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * The message to send. |
| 29 | + * |
| 30 | + * This can be anything, but please set the `Content-Type` header accordingly. |
| 31 | + * |
| 32 | + * You can leave this empty if you want to send a message with no body. |
| 33 | + */ |
| 34 | + body?: BodyInit; |
| 35 | + |
| 36 | + /** |
| 37 | + * Optionally specify a cron expression to repeatedly send this message to the destination. |
| 38 | + * |
| 39 | + * @default undefined |
| 40 | + */ |
| 41 | + cron?: string; |
| 42 | + |
| 43 | + /** |
| 44 | + * Optionally send along headers with the message. |
| 45 | + * These headers will be sent to your destination. |
| 46 | + * |
| 47 | + * We highly recommend sending a `Content-Type` header along, as this will help your destination |
| 48 | + * server to understand the content of the message. |
| 49 | + */ |
| 50 | + headers?: HeadersInit; |
| 51 | + |
| 52 | + /** |
| 53 | + * Optionally delay the delivery of this message. |
| 54 | + * |
| 55 | + * In seconds. |
| 56 | + * |
| 57 | + * @default undefined |
| 58 | + */ |
| 59 | + delay?: number; |
| 60 | + |
| 61 | + /** |
| 62 | + * Optionally set the absolute delay of this message. |
| 63 | + * This will override the delay option. |
| 64 | + * The message will not delivered until the specified time. |
| 65 | + * |
| 66 | + * Unix timestamp in seconds. |
| 67 | + * |
| 68 | + * @default undefined |
| 69 | + */ |
| 70 | + notBefore?: number; |
| 71 | + |
| 72 | + /** |
| 73 | + * We will no longer try to deliver the message after this time |
| 74 | + * |
| 75 | + * Unix timestamp with second precicion |
| 76 | + * |
| 77 | + * @default undefined |
| 78 | + */ |
| 79 | + deadline?: number; |
| 80 | + |
| 81 | + /** |
| 82 | + * Provide a unique id for deduplication. This id will be used to detect duplicate messages. |
| 83 | + * If a duplicate message is detected, the request will be accepted but not enqueued. |
| 84 | + * |
| 85 | + * We store deduplication ids for 90 days. Afterwards it is possible that the message with the |
| 86 | + * same deduplication id is delivered again. |
| 87 | + * |
| 88 | + * When scheduling a message, the deduplication happens before the schedule is created. |
| 89 | + * |
| 90 | + * @default undefined |
| 91 | + */ |
| 92 | + deduplicationID?: string; |
| 93 | + |
| 94 | + /** |
| 95 | + * If true, the message content will get hashed and used as deduplication id. |
| 96 | + * If a duplicate message is detected, the request will be accepted but not enqueued. |
| 97 | + * |
| 98 | + * The content based hash includes the following values: |
| 99 | + * - All headers, except Upstash-Authorization, this includes all headers you are sending. |
| 100 | + * - The entire raw request body The destination from the url path |
| 101 | + * |
| 102 | + * We store deduplication ids for 90 days. Afterwards it is possible that the message with the |
| 103 | + * same deduplication id is delivered again. |
| 104 | + * |
| 105 | + * When scheduling a message, the deduplication happens before the schedule is created. |
| 106 | + * |
| 107 | + * @default false |
| 108 | + */ |
| 109 | + contentBasedDeduplication?: boolean; |
| 110 | + |
| 111 | + /** |
| 112 | + * In case your destination server is unavaialble or returns a status code outside of the 200-299 |
| 113 | + * range, we will retry the request after a certain amount of time. |
| 114 | + * |
| 115 | + * Configure how many times you would like the delivery to be retried |
| 116 | + * |
| 117 | + * @default The maximum retry quota associated with your account. |
| 118 | + */ |
| 119 | + retries?: number; |
| 120 | +}; |
| 121 | + |
| 122 | +export class Client { |
| 123 | + public http: Requester; |
| 124 | + |
| 125 | + public constructor(config: ClientConfig) { |
| 126 | + this.http = new HttpClient({ |
| 127 | + baseUrl: config.baseUrl |
| 128 | + ? config.baseUrl.replace(/\/$/, "") |
| 129 | + : "https://qstash.upstash.io", |
| 130 | + authorization: config.authorization, |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + public get topics(): Topics { |
| 135 | + return new Topics(this.http); |
| 136 | + } |
| 137 | + public get endpoints(): Endpoints { |
| 138 | + return new Endpoints(this.http); |
| 139 | + } |
| 140 | + |
| 141 | + public async publish<R extends PublishRequest = PublishRequest>( |
| 142 | + req: R, |
| 143 | + ): Promise<PublishResponse<R>> { |
| 144 | + const res = await this.http.request<PublishResponse<R>>({ |
| 145 | + path: ["v1", "publish", req.destination], |
| 146 | + body: req.cron, |
| 147 | + headers: req.headers, |
| 148 | + method: "POST", |
| 149 | + }); |
| 150 | + return res; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +type PublishResponse<PublishRequest> = PublishRequest extends { cron: string } |
| 155 | + ? { scheduleID: string } |
| 156 | + : { messageID: string }; |
0 commit comments