diff --git a/.changeset/afraid-candles-enjoy.md b/.changeset/afraid-candles-enjoy.md new file mode 100644 index 00000000..efd62d7a --- /dev/null +++ b/.changeset/afraid-candles-enjoy.md @@ -0,0 +1,5 @@ +--- +"@defer/client": minor +--- + +Remove running state diff --git a/.changeset/brave-jars-yawn.md b/.changeset/brave-jars-yawn.md new file mode 100644 index 00000000..eb91db5f --- /dev/null +++ b/.changeset/brave-jars-yawn.md @@ -0,0 +1,15 @@ +--- +"@defer/client": minor +--- + +Add cancel execution: + +```ts +import { cancelExecution } from "@defer/client"; + +// ... + +const { id } = await cancelExecution(executionId); + +// ... +``` diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 913d395c..8cc7ff58 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,6 +11,7 @@ jobs: name: "Node ${{matrix.node_version}}" runs-on: "ubuntu-latest" strategy: + fail-fast: false matrix: node-version: [16, 18, 20] steps: diff --git a/package.json b/package.json index a301d396..284414e6 100644 --- a/package.json +++ b/package.json @@ -96,4 +96,4 @@ "directory": "dist", "access": "public" } -} \ No newline at end of file +} diff --git a/src/cancelExecution.ts b/src/cancelExecution.ts new file mode 100644 index 00000000..e97e7162 --- /dev/null +++ b/src/cancelExecution.ts @@ -0,0 +1,13 @@ +import { + cancelExecution as cancelExecution2, + CancelExecutionResponse, +} from "./client.js"; +import { __httpClient } from "./index.js"; + +export async function cancelExecution( + id: string +): Promise { + if (__httpClient) return cancelExecution2(__httpClient, { id }); + + return {}; +} diff --git a/src/client.ts b/src/client.ts index 21b826f0..a0facd62 100644 --- a/src/client.ts +++ b/src/client.ts @@ -18,12 +18,7 @@ export interface FetchExecutionRequest { id: string; } -export type ExecutionState = - | "running" - | "created" - | "failed" - | "succeed" - | "started"; +export type ExecutionState = "created" | "failed" | "succeed" | "started"; // eslint-disable-next-line @typescript-eslint/no-explicit-any export interface FetchExecutionResponse { @@ -37,6 +32,20 @@ export type PoolExecutionRequest = FetchExecutionRequest; export type PoolExecutionResponse = FetchExecutionResponse; +export type CancelExecutionRequest = FetchExecutionRequest; + +export interface CancelExecutionResponse {} + +export function cancelExecution( + client: HTTPClient, + request: CancelExecutionRequest +): Promise { + return client( + "POST", + `/public/v1/executions/${request.id}/cancel` + ); +} + export function enqueueExecution( client: HTTPClient, request: EnqueueExecutionRequest diff --git a/src/getExecution.ts b/src/getExecution.ts index 5b1a86a9..47728b29 100644 --- a/src/getExecution.ts +++ b/src/getExecution.ts @@ -13,9 +13,8 @@ export async function getExecution( if (response) return Promise.resolve({ ...response, - // TODO: bump a major and drop `running` state (invalid API state) - state: response.state === "started" ? "running" : response.state, + state: response.state, }); - throw new APIError("execution not found", ""); + throw new APIError("execution not found", "not found"); } diff --git a/src/index.ts b/src/index.ts index 2915f92c..a4d031db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import { DeferError } from "./errors.js"; import { HTTPClient, makeHTTPClient } from "./httpClient.js"; export { getExecution } from "./getExecution.js"; +export { cancelExecution } from "./cancelExecution.js"; interface Options { accessToken?: string; @@ -251,7 +252,7 @@ export const defer: Defer = (fn, options) => { console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); const id = randomUUID(); - __database.set(id, { id: id, state: "running" }); + __database.set(id, { id: id, state: "started" }); execLocally(id, fn, functionArguments); return { id }; }; @@ -334,7 +335,7 @@ export const delay: DeferDelay = (deferFn, delay) => { console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); const id = randomUUID(); - __database.set(id, { id: id, state: "running" }); + __database.set(id, { id: id, state: "started" }); execLocally(id, fn, functionArguments); return { id }; }; @@ -403,7 +404,7 @@ export const addMetadata: DeferAddMetadata = (deferFn, metadata) => { console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); const id = randomUUID(); - __database.set(id, { id: id, state: "running" }); + __database.set(id, { id: id, state: "started" }); execLocally(id, fn, functionArguments); return { id }; }; @@ -455,7 +456,7 @@ export const awaitResult: DeferAwaitResult = response = await waitExecutionResult(__httpClient, { id: id }); } else { const id = randomUUID(); - __database.set(id, { id: id, state: "running" }); + __database.set(id, { id: id, state: "started" }); response = await execLocally(id, fn, functionArguments); } diff --git a/src/next/useDeferRoute.ts b/src/next/useDeferRoute.ts index 9c7de909..52ec3fa8 100644 --- a/src/next/useDeferRoute.ts +++ b/src/next/useDeferRoute.ts @@ -77,7 +77,7 @@ export const useDeferRoute = < return [ execute, { - loading: !!status && ["started", "created", "running"].includes(status), + loading: !!status && ["started", "created"].includes(status), result, error, }, diff --git a/src/version.ts b/src/version.ts index 9e9329c2..436eb765 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default "1.7.2"; +export default "unknow"; diff --git a/tests/awaitResult.spec.ts b/tests/awaitResult.spec.ts index 670fb785..ca3d095a 100644 --- a/tests/awaitResult.spec.ts +++ b/tests/awaitResult.spec.ts @@ -31,7 +31,7 @@ describe("awaitResult(deferFn)", () => { }) // second `/runs/:id` call .mockImplementationOnce(() => { - return { id: "1", state: "running" }; + return { id: "1", state: "started" }; }) // third `/runs/:id` call .mockImplementationOnce(() => {