Skip to content

Commit

Permalink
Merge pull request #162 from apivideo/fix-nodejs-imports
Browse files Browse the repository at this point in the history
Fix nodejs imports
  • Loading branch information
olivier-lando authored Mar 1, 2023
2 parents c5d6cec + 12717a3 commit d277048
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 23 deletions.
2 changes: 2 additions & 0 deletions config/nodejs.yaml
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 2 additions & 1 deletion templates/nodejs/README.md.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion templates/nodejs/fixup
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cat >lib/cjs/package.json <<!EOF
}
!EOF

cat >lib/mjs/package.json <<!EOF
cat >lib/package.json <<!EOF
{
"type": "module"
}
Expand Down
10 changes: 5 additions & 5 deletions templates/nodejs/package.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
"type": "git",
"url": "https://github.com/apivideo/{{gitRepoId}}"
},
"main": "lib/cjs/index.js",
"module": "lib/mjs/index.js",
"main": "lib/cjs/index-cjs.js",
"module": "lib/index.js",
"exports": {
".": {
"import": "./lib/mjs/index.js",
"require": "./lib/cjs/index.js"
"import": "./lib/index.js",
"require": "./lib/cjs/index-cjs.js"
}
},
"typings": "./lib/mjs/index.d.ts",
"typings": "./lib/index.d.ts",
"files": [
"lib/",
"doc/",
Expand Down
2 changes: 1 addition & 1 deletion templates/nodejs/src/ApiVideoError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Response } from 'got';
import ProblemDetails from './model/ProblemDetails';
import ProblemDetails from './model/ProblemDetails.js';

export default class ApiVideoError extends Error {
private problemDetails: ProblemDetails;
Expand Down
6 changes: 3 additions & 3 deletions templates/nodejs/src/HttpClient.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import got, {
Headers,
RequestError,
} from 'got';
import ObjectSerializer from './ObjectSerializer';
import ApiVideoError from './ApiVideoError';
import AccessToken from './model/AccessToken';
import ObjectSerializer from './ObjectSerializer.js';
import ApiVideoError from './ApiVideoError.js';
import AccessToken from './model/AccessToken.js';
import { Readable, Stream } from 'stream';
import FormData from 'form-data';

Expand Down
2 changes: 1 addition & 1 deletion templates/nodejs/src/ObjectSerializer.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#models}}
{{#model}}
import {{classname}}{{#hasEnums}}, { {{#vars}}{{#isEnum}}{{classname}}{{enumName}}, {{/isEnum}}{{/vars}} }{{/hasEnums}} from './model/{{{ classFilename }}}';
import {{classname}}{{#hasEnums}}, { {{#vars}}{{#isEnum}}{{classname}}{{enumName}}, {{/isEnum}}{{/vars}} }{{/hasEnums}} from './model/{{{ classFilename }}}.js';
{{/model}}
{{/models}}

Expand Down
10 changes: 5 additions & 5 deletions templates/nodejs/src/api/api.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {
import { promisify } from 'util';
import { URLSearchParams } from 'url';
import FormData from 'form-data';
import ObjectSerializer from '../ObjectSerializer';
import HttpClient, { QueryOptions } from '../HttpClient';
import ProgressiveSession from '../model/ProgressiveSession';
import ObjectSerializer from '../ObjectSerializer.js';
import HttpClient, { QueryOptions } from '../HttpClient.js';
import ProgressiveSession from '../model/ProgressiveSession.js';
{{#imports}}
import {{classname}} from '../model/{{classname}}';
import {{classname}} from '../model/{{classname}}.js';
{{/imports}}
import { Readable, Stream } from 'stream';
import { Blob } from 'buffer';
import { readableToBuffer } from "../HttpClient";
import { readableToBuffer } from "../HttpClient.js";
{{#operations}}

/**
Expand Down
81 changes: 81 additions & 0 deletions templates/nodejs/src/index-cjs.ts.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{{>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;
10 changes: 8 additions & 2 deletions templates/nodejs/src/index.ts.mustache
Original file line number Diff line number Diff line change
@@ -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 }};
Expand Down Expand Up @@ -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;
4 changes: 2 additions & 2 deletions templates/nodejs/src/model/model.ts.mustache
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down
3 changes: 3 additions & 0 deletions templates/nodejs/tsconfig-cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
},
"include": [
"./src"
],
"exclude": [
"./src/index.ts"
]
}
6 changes: 5 additions & 1 deletion templates/nodejs/tsconfig-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"target": "es2015"
},
"include": [
"./test/"
"./src",
"./test"
],
"exclude": [
"./src/index.ts"
]
}
5 changes: 4 additions & 1 deletion templates/nodejs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"baseUrl": "src",
"declaration": true,
"module": "esnext",
"outDir": "lib/mjs",
"outDir": "lib",
"target": "esnext"
},
"include": [
"./src"
],
"exclude": [
"./src/index-cjs.ts"
]
}

0 comments on commit d277048

Please sign in to comment.