Skip to content

Commit

Permalink
feat: Support ESM (#40)
Browse files Browse the repository at this point in the history
This change allows better module tree shaking for bundlers to produce even smaller bundle sizes.
  • Loading branch information
viveknair committed Jul 2, 2023
1 parent 0d9fd62 commit 8e1b1dd
Show file tree
Hide file tree
Showing 6 changed files with 761 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-please.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: "18.15.0"
node-version: "18.16.1"
cache: "yarn"
cache-dependency-path: yarn.lock
if: ${{ steps.release.outputs.release_created }}
Expand Down
200 changes: 110 additions & 90 deletions configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,128 @@
* These API routes are designed to ingest events from clients.
*
* The version of the OpenAPI document: 0.4.8
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


const packageJson = require("../package.json");

export interface ConfigurationParameters {
clientToken?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
username?: string;
password?: string;
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
basePath?: string;
baseOptions?: any;
formDataCtor?: new () => any;
clientToken?:
| string
| Promise<string>
| ((name: string) => string)
| ((name: string) => Promise<string>);
username?: string;
password?: string;
accessToken?:
| string
| Promise<string>
| ((name?: string, scopes?: string[]) => string)
| ((name?: string, scopes?: string[]) => Promise<string>);
basePath?: string;
baseOptions?: any;
formDataCtor?: new () => any;
}

export class Configuration {
/**
* parameter for clientToken security
* @param name security name
* @memberof Configuration
*/
clientToken?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
/**
* parameter for basic security
*
* @type {string}
* @memberof Configuration
*/
username?: string;
/**
* parameter for basic security
*
* @type {string}
* @memberof Configuration
*/
password?: string;
/**
* parameter for oauth2 security
* @param name security name
* @param scopes oauth2 scope
* @memberof Configuration
*/
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
/**
* override base path
*
* @type {string}
* @memberof Configuration
*/
basePath?: string;
/**
* base options for axios calls
*
* @type {any}
* @memberof Configuration
*/
baseOptions?: any;
/**
* The FormData constructor that will be used to create multipart form data
* requests. You can inject this here so that execution environments that
* do not support the FormData class can still run the generated client.
*
* @type {new () => FormData}
*/
formDataCtor?: new () => any;
/**
* parameter for clientToken security
* @param name security name
* @memberof Configuration
*/
clientToken?:
| string
| Promise<string>
| ((name: string) => string)
| ((name: string) => Promise<string>);
/**
* parameter for basic security
*
* @type {string}
* @memberof Configuration
*/
username?: string;
/**
* parameter for basic security
*
* @type {string}
* @memberof Configuration
*/
password?: string;
/**
* parameter for oauth2 security
* @param name security name
* @param scopes oauth2 scope
* @memberof Configuration
*/
accessToken?:
| string
| Promise<string>
| ((name?: string, scopes?: string[]) => string)
| ((name?: string, scopes?: string[]) => Promise<string>);
/**
* override base path
*
* @type {string}
* @memberof Configuration
*/
basePath?: string;
/**
* base options for axios calls
*
* @type {any}
* @memberof Configuration
*/
baseOptions?: any;
/**
* The FormData constructor that will be used to create multipart form data
* requests. You can inject this here so that execution environments that
* do not support the FormData class can still run the generated client.
*
* @type {new () => FormData}
*/
formDataCtor?: new () => any;

constructor(param: ConfigurationParameters = {}) {
this.clientToken = param.clientToken;
this.username = param.username;
this.password = param.password;
this.accessToken = param.accessToken;
this.basePath = param.basePath;
this.baseOptions = param.baseOptions;
this.formDataCtor = param.formDataCtor;
constructor(param: ConfigurationParameters = {}) {
this.clientToken = param.clientToken;
this.username = param.username;
this.password = param.password;
this.accessToken = param.accessToken;
this.basePath = param.basePath;
this.baseOptions = param.baseOptions;
this.formDataCtor = param.formDataCtor;

if (!this.baseOptions) {
this.baseOptions = {};
}
this.baseOptions.headers = {
'User-Agent': `OpenAI/NodeJS/${packageJson.version}`,
'Authorization': `Bearer ${this.clientToken}`,
...this.baseOptions.headers,
}
if (!this.baseOptions) {
this.baseOptions = {};
}
this.baseOptions.headers = {
"User-Agent": `OpenAI/NodeJS`,
Authorization: `Bearer ${this.clientToken}`,
...this.baseOptions.headers,
};
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp(
"^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$",
"i"
);
return (
mime !== null &&
(jsonMime.test(mime) ||
mime.toLowerCase() === "application/json-patch+json")
);
}
}
37 changes: 29 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@
],
"author": "Gentrace",
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./index.js",
"module": "./index.mjs",
"types": "./index.d.ts",
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js",
"default": "./index.js",
"types": "./index.d.ts"
}
},
"lint-staged": {
"**/*.{ts,tsx,json}": [
"prettier --write"
]
},
"husky": {
"hooks": {
"commit-msg": "npx commitlint -E HUSKY_GIT_PARAMS "
Expand All @@ -25,20 +39,27 @@
]
},
"scripts": {
"build": "run-s clean compile",
"bump-patch": "./scripts/bump-patch",
"clean": "rm -rf dist",
"compile": "tsc -p tsconfig.json "
"build": "run-s clean typescript:compile rollup:compile copy-package",
"clean": "rimraf dist",
"typescript:compile": "tsc -p tsconfig.json",
"rollup:compile": "rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
"copy-package": "cp package.json dist/package.json"
},
"dependencies": {
"axios": "^0.27.2"
},
"devDependencies": {
"@commitlint/cli": "^17.5.1",
"@commitlint/config-conventional": "^17.4.4",
"husky": "^4.3.8",
"@rollup/plugin-commonjs": "^24.1.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-typescript": "^11.1.0",
"@types/node": "^12.11.5",
"husky": "^4.3.8",
"npm-run-all": "^4.1.5",
"rimraf": "^5.0.1",
"rollup": "^3.21.5",
"typescript": "^3.6.4",
"npm-run-all": "^4.1.5"
"lint-staged": "^13.2.0"
}
}
22 changes: 22 additions & 0 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";

export default {
input: ["dist/index.js"],
output: [
{
dir: "dist",
format: "cjs",
preserveModules: true,
sourcemap: true,
},
{
dir: "dist",
format: "es",
sourcemap: true,
preserveModules: true,
entryFileNames: "[name].mjs",
},
],
plugins: [json(), commonjs()],
};
15 changes: 6 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{
"compilerOptions": {
"module": "ESNext",
"declaration": true,
"outDir": "dist",
"target": "ES6",
"module": "commonjs",
"noImplicitAny": true,
"outDir": "dist",
"rootDir": ".",
"moduleResolution": "node",
"typeRoots": [
"node_modules/@types"
]
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"typeRoots": ["node_modules/@types"]
},
"exclude": [
"dist",
"node_modules"
]
"exclude": ["dist", "node_modules"]
}

0 comments on commit 8e1b1dd

Please sign in to comment.