Skip to content

Commit

Permalink
Merge pull request #173 from upstash/add-ts-delay-support
Browse files Browse the repository at this point in the history
feat: add typesafe duration for delays
  • Loading branch information
ogzhanolguncu authored Sep 4, 2024
2 parents abe03f9 + 2ed367e commit 8df2bca
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DLQ } from "./dlq";
import type { Duration } from "./duration";
import { HttpClient, type Requester, type RetryConfig } from "./http";
import { Chat } from "./llm/chat";
import type { ProviderReturnType } from "./llm/providers";
Expand Down Expand Up @@ -61,7 +62,7 @@ export type PublishRequest<TBody = BodyInit> = {
*
* @default undefined
*/
delay?: number;
delay?: Duration | number;

/**
* Optionally set the absolute delay of this message.
Expand Down Expand Up @@ -141,7 +142,7 @@ export type PublishRequest<TBody = BodyInit> = {
*
* @default undefined
*/
timeout?: number;
timeout?: Duration | number;
} & (
| {
/**
Expand Down
4 changes: 4 additions & 0 deletions src/client/duration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Unit = "s" | "m" | "h" | "d";

// Using "bigint" instead of "number" as number allows "20 s" while bigint does not.
export type Duration = `${bigint}${Unit}`;
2 changes: 1 addition & 1 deletion src/client/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("Messages", () => {
url: `https://example.com`,
body: { hello: "world" },
timeout: 90,
delay: 10,
delay: "10d",
},
]);

Expand Down
23 changes: 19 additions & 4 deletions src/client/schedules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { prefixHeaders } from "./utils";
import type { Requester } from "./http";
import type { BodyInit, HeadersInit, HTTPMethods } from "./types";
import type { Duration } from "./duration";

export type Schedule = {
scheduleId: string;
Expand Down Expand Up @@ -50,7 +51,7 @@ export type CreateScheduleRequest = {
*
* @default undefined
*/
delay?: number;
delay?: Duration | number;

/**
* In case your destination server is unavailable or returns a status code outside of the 200-299
Expand Down Expand Up @@ -102,7 +103,7 @@ export type CreateScheduleRequest = {
*
* @default undefined
*/
timeout?: number;
timeout?: Duration | number;

/**
* Schedule id to use.
Expand Down Expand Up @@ -139,7 +140,14 @@ export class Schedules {
}

if (request.delay !== undefined) {
headers.set("Upstash-Delay", `${request.delay.toFixed(0)}s`);
// Handle both string (Duration type) and number inputs for delay
if (typeof request.delay === "string") {
// If delay is a string (e.g., "20s", "1h"), use it directly
headers.set("Upstash-Delay", request.delay);
} else {
// If delay is a number, convert it to seconds and append 's'
headers.set("Upstash-Delay", `${request.delay.toFixed(0)}s`);
}
}

if (request.retries !== undefined) {
Expand All @@ -155,7 +163,14 @@ export class Schedules {
}

if (request.timeout !== undefined) {
headers.set("Upstash-Timeout", `${request.timeout}s`);
// Handle both string (Duration type) and number inputs for timeout
if (typeof request.timeout === "string") {
// If timeout is a string (e.g., "20s", "1h"), use it directly
headers.set("Upstash-Timeout", request.timeout);
} else {
// If timeout is a number, convert it to seconds and append 's'
headers.set("Upstash-Timeout", `${request.timeout}s`);
}
}

if (request.scheduleId !== undefined) {
Expand Down
18 changes: 16 additions & 2 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export function processHeaders(request: PublishRequest) {
headers.set("Upstash-Method", request.method ?? "POST");

if (request.delay !== undefined) {
headers.set("Upstash-Delay", `${request.delay.toFixed(0)}s`);
// Handle both string (Duration type) and number inputs for delay
if (typeof request.delay === "string") {
// If delay is a string (e.g., "20s", "1h"), use it directly
headers.set("Upstash-Delay", request.delay);
} else {
// If delay is a number, convert it to seconds and append 's'
headers.set("Upstash-Delay", `${request.delay.toFixed(0)}s`);
}
}

if (request.notBefore !== undefined) {
Expand Down Expand Up @@ -56,7 +63,14 @@ export function processHeaders(request: PublishRequest) {
}

if (request.timeout !== undefined) {
headers.set("Upstash-Timeout", `${request.timeout}s`);
// Handle both string (Duration type) and number inputs for timeout
if (typeof request.timeout === "string") {
// If timeout is a string (e.g., "20s", "1h"), use it directly
headers.set("Upstash-Timeout", request.timeout);
} else {
// If timeout is a number, convert it to seconds and append 's'
headers.set("Upstash-Timeout", `${request.timeout}s`);
}
}

return headers;
Expand Down

0 comments on commit 8df2bca

Please sign in to comment.