Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getExecution(id) #36

Merged
merged 4 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .changeset/beige-worms-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"@defer/client": minor
---

expose `getExecution(id)` to poll for an execution status and result:

```ts
import { type FetchExecutionResponse, getExecution } from "@defer/client";
import type { NextApiRequest, NextApiResponse } from "next";

type Response = {
res: FetchExecutionResponse;
};

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Response>
) {
const executionId = req.query.id;
const ret = await getExecution(executionId as string);
res.status(200).json({ res: ret });
}
```
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const FakeID = "00000000000000000000000000000000";
import {
enqueueExecution,
EnqueueExecutionResponse,
fetchExecution,
waitExecutionResult,
} from "./client.js";
import { DeferError } from "./errors.js";
import { HTTPClient, makeHTTPClient } from "./httpClient.js";

export type { FetchExecutionResponse } from "./client";

interface Options {
accessToken?: string;
endpoint?: string;
Expand All @@ -37,6 +40,17 @@ export function configure(opts = {} as Options): void {
return;
}

export const deferEnabled = () => !!__accessToken;

export const getExecution = (id: string) => {
if (!__httpClient) {
throw new DeferError(
"getExecution() is not yet supported in development mode; Please rely on deferEnabled() to conditionally use it."
);
}
return fetchExecution(__httpClient, { id });
};

export type UnPromise<F> = F extends Promise<infer R> ? R : F;

export type DelayString = `${string}${Units}`;
Expand Down