Skip to content

Commit 29e7d93

Browse files
committed
chore: remove esm build
1 parent 1cb4395 commit 29e7d93

File tree

7 files changed

+43
-48
lines changed

7 files changed

+43
-48
lines changed

templates/node/package.json.twig

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,12 @@
1111
},
1212
"exports": {
1313
".": {
14-
"import": {
15-
"types": "./dist/index.d.mts",
16-
"default": "./dist/index.mjs"
17-
},
1814
"require": {
1915
"types": "./dist/index.d.ts",
2016
"default": "./dist/index.js"
2117
}
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-
}
3218
}
3319
},
34-
"module": "dist/index.mjs",
3520
"types": "dist/index.d.ts",
3621
"repository": {
3722
"type": "git",

templates/node/src/client.ts.twig

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

44
type Payload = {
@@ -31,9 +31,23 @@ 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+
3446
class Client {
3547
static CHUNK_SIZE = 1024 * 1024 * 5;
3648

49+
fetch = defaultFetch;
50+
3751
config = {
3852
endpoint: '{{ spec.endpoint }}',
3953
selfSigned: false,
@@ -46,7 +60,7 @@ class Client {
4660
'x-sdk-platform': '{{ sdk.platform }}',
4761
'x-sdk-language': '{{ language.name | caseLower }}',
4862
'x-sdk-version': '{{ sdk.version }}',
49-
'user-agent' : '{{spec.title | caseUcfirst}}{{language.name | caseUcfirst}}SDK/{{ sdk.version }}',
63+
'user-agent' : getUserAgent(),
5064
{%~ for key,header in spec.global.defaultHeaders %}
5165
'{{key}}': '{{header}}',
5266
{%~ endfor %}
@@ -67,15 +81,8 @@ class Client {
6781
return this;
6882
}
6983

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;
84+
setFetch(fetch: any): this {
85+
this.fetch = fetch;
7986

8087
return this;
8188
}
@@ -204,7 +211,7 @@ class Client {
204211
async redirect(method: string, url: URL, headers: Headers = {}, params: Payload = {}): Promise<string> {
205212
const { uri, options } = this.prepareRequest(method, url, headers, params);
206213

207-
const response = await fetch(uri, {
214+
const response = await this.fetch(uri, {
208215
...options,
209216
redirect: 'manual'
210217
});
@@ -221,7 +228,7 @@ class Client {
221228

222229
let data: any = null;
223230

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

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

templates/node/src/index.ts.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ 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: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import { File } from "node-fetch-native";
2-
import { realpathSync, readFileSync } from "fs";
3-
import type { BinaryLike } from "crypto";
2+
import { BinaryLike } from "crypto";
43

54
export class InputFile {
5+
static fromBlob(blob: Blob, name: string): File {
6+
return new File([blob], name);
7+
}
8+
69
static fromBuffer(
7-
parts: Blob | BinaryLike,
10+
parts: BinaryLike,
811
name: string
912
): File {
1013
return new File([parts], name);
1114
}
1215

1316
static fromPath(path: string, name: string): File {
14-
const realPath = realpathSync(path);
15-
const contents = readFileSync(realPath);
16-
return this.fromBuffer(contents, name);
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');
1726
}
1827

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

templates/node/tsconfig.json.twig

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

templates/node/tsup.config.js.twig

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

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

tests/languages/node/test.js

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

2322
const foo = new Foo(client);
2423
const bar = new Bar(client);

0 commit comments

Comments
 (0)