From 12717a399138449cdbe6afc59211e95380c53ecd Mon Sep 17 00:00:00 2001 From: Olivier Lando Date: Wed, 1 Mar 2023 17:53:02 +0100 Subject: [PATCH] Fix nodejs imports --- config/nodejs.yaml | 2 + templates/nodejs/README.md.mustache | 3 +- templates/nodejs/fixup | 2 +- templates/nodejs/package.json.mustache | 10 +-- templates/nodejs/src/ApiVideoError.ts | 2 +- templates/nodejs/src/HttpClient.ts.mustache | 6 +- .../nodejs/src/ObjectSerializer.ts.mustache | 2 +- templates/nodejs/src/api/api.ts.mustache | 10 +-- templates/nodejs/src/index-cjs.ts.mustache | 81 +++++++++++++++++++ templates/nodejs/src/index.ts.mustache | 10 ++- templates/nodejs/src/model/model.ts.mustache | 4 +- templates/nodejs/tsconfig-cjs.json | 3 + templates/nodejs/tsconfig-test.json | 6 +- templates/nodejs/tsconfig.json | 5 +- 14 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 templates/nodejs/src/index-cjs.ts.mustache diff --git a/config/nodejs.yaml b/config/nodejs.yaml index 6b2c6d01..c2814b06 100644 --- a/config/nodejs.yaml +++ b/config/nodejs.yaml @@ -1,4 +1,6 @@ changelog: + - 2.3.2 (2023-03-01): + - fix import issues - 2.3.1 (2023-01-18): - fix unit tests - 2.3.0 (2023-01-18): diff --git a/templates/nodejs/README.md.mustache b/templates/nodejs/README.md.mustache index 6a185f66..9d499e23 100644 --- a/templates/nodejs/README.md.mustache +++ b/templates/nodejs/README.md.mustache @@ -54,7 +54,8 @@ npm run build ## Code sample ```typescript -const ApiVideoClient = require('@api.video/nodejs-client').default; +const ApiVideoClient = require('@api.video/nodejs-client'); +// or: import ApiVideoClient from '@api.video/nodejs-client'; (async () => { try { diff --git a/templates/nodejs/fixup b/templates/nodejs/fixup index 6e8fbf0b..5dcf0929 100755 --- a/templates/nodejs/fixup +++ b/templates/nodejs/fixup @@ -9,7 +9,7 @@ cat >lib/cjs/package.json <lib/mjs/package.json <lib/package.json <licenseInfo}} + +import HttpClient from "./HttpClient.js"; +{{# apiInfo.apis }}{{^x-client-hidden}} +import {{classFilename}} from './api/{{classFilename}}.js';{{/x-client-hidden}}{{/ apiInfo.apis }} + +{{#models}}{{#model}} + import {{classname}} from './model/{{classname}}.js';{{/model}}{{/models}} + +const PRODUCTION_BASE_URI = "https://ws.api.video"; +const DEFAULT_CHUNK_SIZE = {{ defaultChunkSize }}; +const MIN_CHUNK_SIZE = {{ minChunkSize }}; +const MAX_CHUNK_SIZE = {{ maxChunkSize }}; + +class ApiVideoClient { + private httpClient: HttpClient;{{# apiInfo.apis }}{{^x-client-hidden}} + private _{{classVarName}}: {{classFilename}};{{/x-client-hidden}}{{/ apiInfo.apis }} + + constructor(params: {apiKey?: string, baseUri?: string, chunkSize?: number, applicationName?: string, applicationVersion?: string; sdkName?: string; sdkVersion?: string;}) { + if(params.chunkSize && (params.chunkSize < MIN_CHUNK_SIZE || params.chunkSize > MAX_CHUNK_SIZE)) { + throw new Error("Invalid chunk size value. Must be greater than " + MIN_CHUNK_SIZE + " bytes and lower than " + MAX_CHUNK_SIZE + " bytes."); + } + + this.validateOrigin( + 'application', + params.applicationName, + params.applicationVersion + ); + + this.validateOrigin('sdk', params.sdkName, params.sdkVersion); + + this.httpClient = new HttpClient({ + ...params, + baseUri: params.baseUri || PRODUCTION_BASE_URI, + chunkSize: params.chunkSize || DEFAULT_CHUNK_SIZE, + }) +{{# apiInfo.apis }}{{^x-client-hidden}} + this._{{classVarName}} = new {{classFilename}}(this.httpClient);{{/x-client-hidden}}{{/ apiInfo.apis }} + } + + public async getAccessToken() { + return this.httpClient.getAccessToken(); + } + +{{# apiInfo.apis }}{{^x-client-hidden}} + /** + * Get an {{classFilename}} instance + * @return {{classFilename}} + */ + public get {{classVarName}}(): {{classFilename}} { + return this._{{classVarName}}; + } +{{/x-client-hidden}}{{/ apiInfo.apis }} + + + private validateOrigin(type: string, name?: string, version?: string) { + if (name && !version) { + throw new Error( + `${type} version is mandatory when ${type} name is set.'` + ); + } else if (!name && version) { + throw new Error( + `${type} name is mandatory when ${type} version is set.'` + ); + } else if (name && version) { + if (!/^[\w-]{1,50}$/.test(name)) { + throw new Error( + `Invalid ${type} name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50.` + ); + } + + if (!/^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$/.test(version)) { + throw new Error( + `Invalid ${type} version value. The version should match the xxx[.yyy][.zzz] pattern.` + ); + } + } + } +} + +export = ApiVideoClient; \ No newline at end of file diff --git a/templates/nodejs/src/index.ts.mustache b/templates/nodejs/src/index.ts.mustache index 4d95a4f3..f35dea93 100644 --- a/templates/nodejs/src/index.ts.mustache +++ b/templates/nodejs/src/index.ts.mustache @@ -1,8 +1,11 @@ {{>licenseInfo}} -import HttpClient from "./HttpClient"; +import HttpClient from "./HttpClient.js"; {{# apiInfo.apis }}{{^x-client-hidden}} -import {{classFilename}} from './api/{{classFilename}}';{{/x-client-hidden}}{{/ apiInfo.apis }} +import {{classFilename}} from './api/{{classFilename}}.js';{{/x-client-hidden}}{{/ apiInfo.apis }} + +{{#models}}{{#model}} + import {{classname}} from './model/{{classname}}.js';{{/model}}{{/models}} const PRODUCTION_BASE_URI = "https://ws.api.video"; const DEFAULT_CHUNK_SIZE = {{ defaultChunkSize }}; @@ -75,4 +78,7 @@ class ApiVideoClient { } } +export { {{# apiInfo.apis }}{{^x-client-hidden}}{{classFilename}}, {{/x-client-hidden}}{{/ apiInfo.apis }} {{#models}}{{#model}}{{classname}}, {{/model}}{{/models}} }; + + export default ApiVideoClient; \ No newline at end of file diff --git a/templates/nodejs/src/model/model.ts.mustache b/templates/nodejs/src/model/model.ts.mustache index e5f1a932..8d75c815 100644 --- a/templates/nodejs/src/model/model.ts.mustache +++ b/templates/nodejs/src/model/model.ts.mustache @@ -1,9 +1,9 @@ {{>licenseInfo}} -import AttributeType from './AttributeType'; +import AttributeType from './AttributeType.js'; {{#models}} {{#model}} {{#tsImports}} -import {{classname}} from './{{filename}}'; +import {{classname}} from './{{filename}}.js'; {{/tsImports}} {{#description}} diff --git a/templates/nodejs/tsconfig-cjs.json b/templates/nodejs/tsconfig-cjs.json index e49ac6eb..42011341 100644 --- a/templates/nodejs/tsconfig-cjs.json +++ b/templates/nodejs/tsconfig-cjs.json @@ -9,5 +9,8 @@ }, "include": [ "./src" + ], + "exclude": [ + "./src/index.ts" ] } diff --git a/templates/nodejs/tsconfig-test.json b/templates/nodejs/tsconfig-test.json index 18932b91..3b75c3ce 100644 --- a/templates/nodejs/tsconfig-test.json +++ b/templates/nodejs/tsconfig-test.json @@ -8,6 +8,10 @@ "target": "es2015" }, "include": [ - "./test/" + "./src", + "./test" + ], + "exclude": [ + "./src/index.ts" ] } diff --git a/templates/nodejs/tsconfig.json b/templates/nodejs/tsconfig.json index c930c77f..57016050 100644 --- a/templates/nodejs/tsconfig.json +++ b/templates/nodejs/tsconfig.json @@ -4,10 +4,13 @@ "baseUrl": "src", "declaration": true, "module": "esnext", - "outDir": "lib/mjs", + "outDir": "lib", "target": "esnext" }, "include": [ "./src" + ], + "exclude": [ + "./src/index-cjs.ts" ] }