Skip to content

Commit

Permalink
Merge pull request #36 from defer-run/feat/fetch-execution
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly authored Mar 5, 2023
2 parents 53fe187 + 8ffcf7d commit 458bad5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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

0 comments on commit 458bad5

Please sign in to comment.