Skip to content

Commit 45919f0

Browse files
committed
Revert "chore: remove esm build"
This reverts commit 29e7d93.
1 parent 7304672 commit 45919f0

File tree

7 files changed

+48
-43
lines changed

7 files changed

+48
-43
lines changed

templates/node/package.json.twig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@
1111
},
1212
"exports": {
1313
".": {
14+
"import": {
15+
"types": "./dist/index.d.mts",
16+
"default": "./dist/index.mjs"
17+
},
1418
"require": {
1519
"types": "./dist/index.d.ts",
1620
"default": "./dist/index.js"
1721
}
22+
},
23+
"./file": {
24+
"import": {
25+
"types": "./dist/inputFile.d.mts",
26+
"default": "./dist/inputFile.mjs"
27+
},
28+
"require": {
29+
"types": "./dist/inputFile.d.ts",
30+
"default": "./dist/inputFile.js"
31+
}
1832
}
1933
},
34+
"module": "dist/index.mjs",
2035
"types": "dist/index.d.ts",
2136
"repository": {
2237
"type": "git",

templates/node/src/client.ts.twig

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetch as defaultFetch, FormData, File } from 'node-fetch-native';
1+
import { fetch, FormData, File } from 'node-fetch-native';
22
import { Models } from './models';
33

44
type Payload = {
@@ -31,23 +31,9 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
3131
}
3232
}
3333

34-
function getUserAgent(): string {
35-
const base = '{{spec.title | caseUcfirst}}{{language.name | caseUcfirst}}SDK/{{ sdk.version }}';
36-
37-
// @ts-ignore
38-
if (typeof globalThis.EdgeRuntime !== 'string') {
39-
const {type, version, arch} = require('os');
40-
return base + `(${type()}\\${version()}\\${arch()})`
41-
}
42-
43-
return base + `(Edge\${globalThis.EdgeRuntime})`;
44-
}
45-
4634
class Client {
4735
static CHUNK_SIZE = 1024 * 1024 * 5;
4836

49-
fetch = defaultFetch;
50-
5137
config = {
5238
endpoint: '{{ spec.endpoint }}',
5339
selfSigned: false,
@@ -60,7 +46,7 @@ class Client {
6046
'x-sdk-platform': '{{ sdk.platform }}',
6147
'x-sdk-language': '{{ language.name | caseLower }}',
6248
'x-sdk-version': '{{ sdk.version }}',
63-
'user-agent' : getUserAgent(),
49+
'user-agent' : '{{spec.title | caseUcfirst}}{{language.name | caseUcfirst}}SDK/{{ sdk.version }}',
6450
{%~ for key,header in spec.global.defaultHeaders %}
6551
'{{key}}': '{{header}}',
6652
{%~ endfor %}
@@ -81,8 +67,15 @@ class Client {
8167
return this;
8268
}
8369

84-
setFetch(fetch: any): this {
85-
this.fetch = fetch;
70+
/**
71+
* Set self-signed
72+
*
73+
* @param {boolean} selfSigned
74+
*
75+
* @returns {this}
76+
*/
77+
setSelfSigned(selfSigned: boolean): this {
78+
this.config.selfSigned = selfSigned;
8679

8780
return this;
8881
}
@@ -211,7 +204,7 @@ class Client {
211204
async redirect(method: string, url: URL, headers: Headers = {}, params: Payload = {}): Promise<string> {
212205
const { uri, options } = this.prepareRequest(method, url, headers, params);
213206

214-
const response = await this.fetch(uri, {
207+
const response = await fetch(uri, {
215208
...options,
216209
redirect: 'manual'
217210
});
@@ -228,7 +221,7 @@ class Client {
228221

229222
let data: any = null;
230223

231-
const response = await this.fetch(uri, options);
224+
const response = await fetch(uri, options);
232225

233226
if (response.headers.get('content-type')?.includes('application/json')) {
234227
data = await response.json();

templates/node/src/index.ts.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ export { ID } from './id';
1010
{% for enum in spec.enums %}
1111
export { {{ enum.name | caseUcfirst }} } from './enums/{{enum.name | caseDash}}';
1212
{% endfor %}
13-
export { InputFile } from './inputFile';

templates/node/src/inputFile.ts.twig

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
import { File } from "node-fetch-native";
2-
import { BinaryLike } from "crypto";
2+
import { realpathSync, readFileSync } from "fs";
3+
import type { BinaryLike } from "crypto";
34

45
export class InputFile {
5-
static fromBlob(blob: Blob, name: string): File {
6-
return new File([blob], name);
7-
}
8-
96
static fromBuffer(
10-
parts: BinaryLike,
7+
parts: Blob | BinaryLike,
118
name: string
129
): File {
1310
return new File([parts], name);
1411
}
1512

1613
static fromPath(path: string, name: string): File {
17-
// @ts-ignore
18-
if (typeof globalThis.EdgeRuntime !== 'string') {
19-
const { realpathSync, readFileSync } = require('fs');
20-
const realPath = realpathSync(path);
21-
const contents = readFileSync(realPath);
22-
return this.fromBuffer(contents, name);
23-
}
24-
25-
throw new Error('node:fs is not available in edge runtimes');
14+
const realPath = realpathSync(path);
15+
const contents = readFileSync(realPath);
16+
return this.fromBuffer(contents, name);
2617
}
2718

2819
static fromPlainText(content: string, name: string): File {

templates/node/tsconfig.json.twig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
22
"compilerOptions": {
3-
"extendedDiagnostics": true,
43
"esModuleInterop": true,
54
"skipLibCheck": true,
6-
"target": "ES6",
5+
"target": "es2017",
6+
"allowJs": true,
7+
"resolveJsonModule": true,
8+
"moduleDetection": "force",
9+
"isolatedModules": true,
710
"strict": true,
8-
"module": "CommonJS",
9-
"moduleResolution": "node",
11+
"module": "es2022",
12+
"moduleResolution": "Bundler",
13+
"lib": ["es2022"]
1014
},
15+
"compileOnSave": false,
1116
"exclude": ["node_modules", "dist"],
1217
"include": ["src"]
1318
}

templates/node/tsup.config.js.twig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { defineConfig } = require("tsup");
1+
import { defineConfig } from 'tsup'
22

3-
module.exports = defineConfig({
3+
export default defineConfig({
44
entry: ["src/**/*.ts"],
55
sourcemap: true,
66
clean: true,
@@ -11,8 +11,9 @@ module.exports = defineConfig({
1111
outExtension({ format }) {
1212
return {
1313
cjs: '.js',
14+
esm: '.mjs'
1415
}[format]
1516
},
1617
target: "node16",
17-
format: ['cjs']
18+
format: ['cjs', 'esm']
1819
})

tests/languages/node/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async function start() {
1818
// Init SDK
1919
const client = new Client()
2020
.addHeader("Origin", "http://localhost")
21+
.setSelfSigned(true);
2122

2223
const foo = new Foo(client);
2324
const bar = new Bar(client);

0 commit comments

Comments
 (0)