Skip to content
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
4 changes: 2 additions & 2 deletions fetch-esm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Fundamental difference from CommonJS:

- [`package.json`](./package.json) has `"type": "module"` attribute.
- [`tsconfig.json`](./tsconfig.json) outputs in `esnext` format.
- Imports [must](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) include the `.js` file extension.
- Imports [must](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) include the `.js` or `.ts` file extension.

### Running this sample

Expand All @@ -18,5 +18,5 @@ Fundamental difference from CommonJS:
The Workflow should return:

```
Hello, Temporal!
Hello World!
```
26 changes: 10 additions & 16 deletions fetch-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,33 @@
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "eslint .",
"start": "node --loader ts-node/esm src/worker.ts",
"start.watch": "nodemon src/worker.ts",
"workflow": "node --loader ts-node/esm src/client.ts"
},
"nodemonConfig": {
"execMap": {
"ts": "node --loader ts-node/esm"
},
"ext": "ts",
"watch": [
"src"
]
"start": "tsx src/worker.ts",
"start.watch": "tsx watch src/worker.ts",
"workflow": "tsx src/client-fetch.ts",
"workflow-fetch": "tsx src/client-fetch.ts",
"workflow-local": "tsx src/client-local.ts"
},
"dependencies": {
"@temporalio/activity": "^1.13.2",
"@temporalio/client": "^1.13.2",
"@temporalio/common": "^1.13.2",
"@temporalio/envconfig": "^1.13.2",
"@temporalio/worker": "^1.13.2",
"@temporalio/workflow": "^1.13.2"
"@temporalio/workflow": "^1.13.2",
"humanize-string": "^3.1.0",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.4",
"@types/node": "^22.9.1",
"@types/node-fetch": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"@typescript-eslint/parser": "^8.18.0",
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-deprecation": "^3.0.0",
"node-fetch": "^3.0.0",
"nodemon": "^3.1.7",
"prettier": "^3.4.2",
"ts-node": "^10.9.2",
"tsx": "^4.20.6",
"typescript": "^5.6.3"
}
}
11 changes: 11 additions & 0 deletions fetch-esm/src/activities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// humanize-string and node-fetch are ESM libraries
import humanizeString from 'humanize-string';
import fetch from 'node-fetch';

import { ApplicationFailure } from '@temporalio/common';

export async function greetHTTP(name: string): Promise<string> {
const response = await fetch('http://httpbin.org/get?greeting=Hello');
if (!response.ok) {
throw ApplicationFailure.retryable(`HTTP error! status: ${response.status}`);
}
const body: any = await response.json();
return `${body.args.greeting}, ${name}!`;
}

export async function greetLocal(name: string): Promise<string> {
return humanizeString(`Hello-World-And-Hello-${name}!`, { preserveCase: true });
}
6 changes: 3 additions & 3 deletions fetch-esm/src/client.ts → fetch-esm/src/client-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @@@SNIPSTART typescript-esm-client
import { Client, Connection } from '@temporalio/client';
import { loadClientConnectConfig } from '@temporalio/envconfig';
import { example } from './workflows.js';
import { exampleFetch } from './workflows.ts';

const config = loadClientConnectConfig();
const connection = await Connection.connect(config.connectionOptions);
const client = new Client({ connection });
const result = await client.workflow.execute(example, {
const result = await client.workflow.execute(exampleFetch, {
taskQueue: 'fetch-esm',
workflowId: 'my-business-id',
workflowId: `my-business-id-${Date.now()}`,
args: ['Temporal'],
});
console.log(result); // Hello, Temporal!
Expand Down
15 changes: 15 additions & 0 deletions fetch-esm/src/client-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @@@SNIPSTART typescript-esm-client
import { Client, Connection } from '@temporalio/client';
import { loadClientConnectConfig } from '@temporalio/envconfig';
import { exampleLocal } from './workflows.ts';

const config = loadClientConnectConfig();
const connection = await Connection.connect(config.connectionOptions);
const client = new Client({ connection });
const result = await client.workflow.execute(exampleLocal, {
taskQueue: 'fetch-esm',
workflowId: `my-business-id-${Date.now()}`,
args: ['Wonderful-Temporal'],
});
console.log(result); // Hello World And Hello Wonderful Temporal!
// @@@SNIPEND
2 changes: 1 addition & 1 deletion fetch-esm/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Worker } from '@temporalio/worker';
import { URL, fileURLToPath } from 'url';
import path from 'path';
import * as activities from './activities.js';
import * as activities from './activities.ts';

// Support running both complied code and ts-node/esm loader
const workflowsPathUrl = new URL(`./workflows${path.extname(import.meta.url)}`, import.meta.url);
Expand Down
24 changes: 20 additions & 4 deletions fetch-esm/src/workflows.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// @@@SNIPSTART typescript-esm-workflow
import { proxyActivities } from '@temporalio/workflow';
// Only import the activity types
import type * as activities from './activities.js';
import type * as activities from './activities.ts';

const { greetLocal } = proxyActivities<typeof activities>({
scheduleToCloseTimeout: '5 seconds',
});

// A variant of the preceding workflow that does not perform any HTTP requests
export async function exampleLocal(name: string): Promise<string> {
return await greetLocal(name);
}
// @@@SNIPEND

// The sample `fetch` activity make requests to httpbin.org, which is sometime slow or flaky.
// In the case of this workflow, we prefer to fail the workflow than retry the HTTP request forever.
const { greetHTTP } = proxyActivities<typeof activities>({
startToCloseTimeout: '1 minute',
startToCloseTimeout: '3 seconds',
retry: {
initialInterval: '500 ms',
maximumAttempts: 3,
backoffCoefficient: 1.5,
},
});

export async function example(name: string): Promise<string> {
export async function exampleFetch(name: string): Promise<string> {
return await greetHTTP(name);
}
// @@@SNIPEND
4 changes: 3 additions & 1 deletion fetch-esm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"declarationMap": true,
"sourceMap": true,
"rootDir": "./src",
"outDir": "./lib"
"outDir": "./lib",
"allowImportingTsExtensions": true,
"noEmit": true
},
"include": ["src/**/*.ts"]
}
Loading
Loading