From 6dad500aa81d4c5a7bb1b07f897208ade2d01971 Mon Sep 17 00:00:00 2001 From: olivierapivideo Date: Tue, 5 Jul 2022 08:35:56 +0000 Subject: [PATCH] feat(ios): add origin sdk field support --- .github/workflows/test.yml | 2 +- CHANGELOG.md | 3 + package-lock.json | 172 ++++++++++++++++++------------------- package.json | 2 +- src/HttpClient.ts | 15 ++-- src/index.ts | 54 +++++++----- test/client.spec.ts | 27 ++++-- 7 files changed, 155 insertions(+), 120 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 89ce8b1..bfed22b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node: ['14', '15'] + node: ['15'] steps: - uses: actions/checkout@v2 - name: Setup node ${{ matrix.node }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d13b3d..cf5e666 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All changes to this project will be documented in this file. +## [2.2.8] - 2022-07-05 +- Add SDK origin header + ## [2.2.7] - 2022-05-06 - Upload methods (except for videos upload) now accept Readable and Buffer in addition to the file path. diff --git a/package-lock.json b/package-lock.json index b803b2e..b241cf9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@api.video/nodejs-client", - "version": "2.2.7", + "version": "2.2.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@api.video/nodejs-client", - "version": "2.2.7", + "version": "2.2.8", "license": "MIT", "dependencies": { "form-data": "^4.0.0", @@ -233,14 +233,14 @@ "dev": true }, "node_modules/@typescript-eslint/eslint-plugin": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.30.4.tgz", - "integrity": "sha512-xjujQISAIa4HAaos8fcMZXmqkuZqMx6icdxkI88jMM/eNe4J8AuTLYnLK+zdm0mBYLyctdFf//UE4/xFCcQzYQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.30.5.tgz", + "integrity": "sha512-lftkqRoBvc28VFXEoRgyZuztyVUQ04JvUnATSPtIRFAccbXTWL6DEtXGYMcbg998kXw1NLUJm7rTQ9eUt+q6Ig==", "dev": true, "dependencies": { - "@typescript-eslint/scope-manager": "5.30.4", - "@typescript-eslint/type-utils": "5.30.4", - "@typescript-eslint/utils": "5.30.4", + "@typescript-eslint/scope-manager": "5.30.5", + "@typescript-eslint/type-utils": "5.30.5", + "@typescript-eslint/utils": "5.30.5", "debug": "^4.3.4", "functional-red-black-tree": "^1.0.1", "ignore": "^5.2.0", @@ -266,14 +266,14 @@ } }, "node_modules/@typescript-eslint/parser": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.30.4.tgz", - "integrity": "sha512-/ge1HtU63wVoED4VnlU2o+FPFmi017bPYpeSrCmd8Ycsti4VSxXrmcpXXm7JpI4GT0Aa7qviabv1PEp6L5bboQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.30.5.tgz", + "integrity": "sha512-zj251pcPXI8GO9NDKWWmygP6+UjwWmrdf9qMW/L/uQJBM/0XbU2inxe5io/234y/RCvwpKEYjZ6c1YrXERkK4Q==", "dev": true, "dependencies": { - "@typescript-eslint/scope-manager": "5.30.4", - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/typescript-estree": "5.30.4", + "@typescript-eslint/scope-manager": "5.30.5", + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/typescript-estree": "5.30.5", "debug": "^4.3.4" }, "engines": { @@ -293,13 +293,13 @@ } }, "node_modules/@typescript-eslint/scope-manager": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.30.4.tgz", - "integrity": "sha512-DNzlQwGSiGefz71JwaHrpcaAX3zYkEcy8uVuan3YMKOa6qeW/y+7SaD8KIsIAruASwq6P+U4BjWBWtM2O+mwBQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.30.5.tgz", + "integrity": "sha512-NJ6F+YHHFT/30isRe2UTmIGGAiXKckCyMnIV58cE3JkHmaD6e5zyEYm5hBDv0Wbin+IC0T1FWJpD3YqHUG/Ydg==", "dev": true, "dependencies": { - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/visitor-keys": "5.30.4" + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/visitor-keys": "5.30.5" }, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -310,12 +310,12 @@ } }, "node_modules/@typescript-eslint/type-utils": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.30.4.tgz", - "integrity": "sha512-55cf1dZviwwv+unDB+mF8vZkfta5muTK6bppPvenWWCD7slZZ0DEsXUjZerqy7Rq8s3J4SXdg4rMIY8ngCtTmA==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.30.5.tgz", + "integrity": "sha512-k9+ejlv1GgwN1nN7XjVtyCgE0BTzhzT1YsQF0rv4Vfj2U9xnslBgMYYvcEYAFVdvhuEscELJsB7lDkN7WusErw==", "dev": true, "dependencies": { - "@typescript-eslint/utils": "5.30.4", + "@typescript-eslint/utils": "5.30.5", "debug": "^4.3.4", "tsutils": "^3.21.0" }, @@ -336,9 +336,9 @@ } }, "node_modules/@typescript-eslint/types": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.30.4.tgz", - "integrity": "sha512-NTEvqc+Vvu8Q6JeAKryHk2eqLKqsr2St3xhIjhOjQv5wQUBhaTuix4WOSacqj0ONWfKVU12Eug3LEAB95GBkMA==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.30.5.tgz", + "integrity": "sha512-kZ80w/M2AvsbRvOr3PjaNh6qEW1LFqs2pLdo2s5R38B2HYXG8Z0PP48/4+j1QHJFL3ssHIbJ4odPRS8PlHrFfw==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -349,13 +349,13 @@ } }, "node_modules/@typescript-eslint/typescript-estree": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.30.4.tgz", - "integrity": "sha512-V4VnEs6/J9/nNizaA12IeU4SAeEYaiKr7XndLNfV5+3zZSB4hIu6EhHJixTKhvIqA+EEHgBl6re8pivBMLLO1w==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.30.5.tgz", + "integrity": "sha512-qGTc7QZC801kbYjAr4AgdOfnokpwStqyhSbiQvqGBLixniAKyH+ib2qXIVo4P9NgGzwyfD9I0nlJN7D91E1VpQ==", "dev": true, "dependencies": { - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/visitor-keys": "5.30.4", + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/visitor-keys": "5.30.5", "debug": "^4.3.4", "globby": "^11.1.0", "is-glob": "^4.0.3", @@ -376,15 +376,15 @@ } }, "node_modules/@typescript-eslint/utils": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.30.4.tgz", - "integrity": "sha512-a+GQrJzOUhn4WT1mUumXDyam+22Oo4c5K/jnZ+6r/4WTQF3q8e4CsC9PLHb4SnOClzOqo/5GLZWvkE1aa5UGKQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.30.5.tgz", + "integrity": "sha512-o4SSUH9IkuA7AYIfAvatldovurqTAHrfzPApOZvdUq01hHojZojCFXx06D/aFpKCgWbMPRdJBWAC3sWp3itwTA==", "dev": true, "dependencies": { "@types/json-schema": "^7.0.9", - "@typescript-eslint/scope-manager": "5.30.4", - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/typescript-estree": "5.30.4", + "@typescript-eslint/scope-manager": "5.30.5", + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/typescript-estree": "5.30.5", "eslint-scope": "^5.1.1", "eslint-utils": "^3.0.0" }, @@ -400,12 +400,12 @@ } }, "node_modules/@typescript-eslint/visitor-keys": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.30.4.tgz", - "integrity": "sha512-ulKGse3mruSc8x6l8ORSc6+1ORyJzKmZeIaRTu/WpaF/jx3vHvEn5XZUKF9XaVg2710mFmTAUlLcLYLPp/Zf/Q==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.30.5.tgz", + "integrity": "sha512-D+xtGo9HUMELzWIUqcQc0p2PO4NyvTrgIOK/VnSH083+8sq0tiLozNRKuLarwHYGRuA6TVBQSuuLwJUDWd3aaA==", "dev": true, "dependencies": { - "@typescript-eslint/types": "5.30.4", + "@typescript-eslint/types": "5.30.5", "eslint-visitor-keys": "^3.3.0" }, "engines": { @@ -1452,9 +1452,9 @@ } }, "node_modules/globals": { - "version": "13.15.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.15.0.tgz", - "integrity": "sha512-bpzcOlgDhMG070Av0Vy5Owklpv1I6+j96GhUI7Rh7IzDCKLzboflLrrfqMu8NquDbiR4EOQk7XzJwqVJxicxog==", + "version": "13.16.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.16.0.tgz", + "integrity": "sha512-A1lrQfpNF+McdPOnnFqY3kSN0AFTy485bTi1bkLk4mVPODIUEcSfhHgRqA+QdXPksrSTTztYXx37NFV+GpGk3Q==", "dev": true, "dependencies": { "type-fest": "^0.20.2" @@ -2963,14 +2963,14 @@ "dev": true }, "@typescript-eslint/eslint-plugin": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.30.4.tgz", - "integrity": "sha512-xjujQISAIa4HAaos8fcMZXmqkuZqMx6icdxkI88jMM/eNe4J8AuTLYnLK+zdm0mBYLyctdFf//UE4/xFCcQzYQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.30.5.tgz", + "integrity": "sha512-lftkqRoBvc28VFXEoRgyZuztyVUQ04JvUnATSPtIRFAccbXTWL6DEtXGYMcbg998kXw1NLUJm7rTQ9eUt+q6Ig==", "dev": true, "requires": { - "@typescript-eslint/scope-manager": "5.30.4", - "@typescript-eslint/type-utils": "5.30.4", - "@typescript-eslint/utils": "5.30.4", + "@typescript-eslint/scope-manager": "5.30.5", + "@typescript-eslint/type-utils": "5.30.5", + "@typescript-eslint/utils": "5.30.5", "debug": "^4.3.4", "functional-red-black-tree": "^1.0.1", "ignore": "^5.2.0", @@ -2980,52 +2980,52 @@ } }, "@typescript-eslint/parser": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.30.4.tgz", - "integrity": "sha512-/ge1HtU63wVoED4VnlU2o+FPFmi017bPYpeSrCmd8Ycsti4VSxXrmcpXXm7JpI4GT0Aa7qviabv1PEp6L5bboQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.30.5.tgz", + "integrity": "sha512-zj251pcPXI8GO9NDKWWmygP6+UjwWmrdf9qMW/L/uQJBM/0XbU2inxe5io/234y/RCvwpKEYjZ6c1YrXERkK4Q==", "dev": true, "requires": { - "@typescript-eslint/scope-manager": "5.30.4", - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/typescript-estree": "5.30.4", + "@typescript-eslint/scope-manager": "5.30.5", + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/typescript-estree": "5.30.5", "debug": "^4.3.4" } }, "@typescript-eslint/scope-manager": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.30.4.tgz", - "integrity": "sha512-DNzlQwGSiGefz71JwaHrpcaAX3zYkEcy8uVuan3YMKOa6qeW/y+7SaD8KIsIAruASwq6P+U4BjWBWtM2O+mwBQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.30.5.tgz", + "integrity": "sha512-NJ6F+YHHFT/30isRe2UTmIGGAiXKckCyMnIV58cE3JkHmaD6e5zyEYm5hBDv0Wbin+IC0T1FWJpD3YqHUG/Ydg==", "dev": true, "requires": { - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/visitor-keys": "5.30.4" + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/visitor-keys": "5.30.5" } }, "@typescript-eslint/type-utils": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.30.4.tgz", - "integrity": "sha512-55cf1dZviwwv+unDB+mF8vZkfta5muTK6bppPvenWWCD7slZZ0DEsXUjZerqy7Rq8s3J4SXdg4rMIY8ngCtTmA==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.30.5.tgz", + "integrity": "sha512-k9+ejlv1GgwN1nN7XjVtyCgE0BTzhzT1YsQF0rv4Vfj2U9xnslBgMYYvcEYAFVdvhuEscELJsB7lDkN7WusErw==", "dev": true, "requires": { - "@typescript-eslint/utils": "5.30.4", + "@typescript-eslint/utils": "5.30.5", "debug": "^4.3.4", "tsutils": "^3.21.0" } }, "@typescript-eslint/types": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.30.4.tgz", - "integrity": "sha512-NTEvqc+Vvu8Q6JeAKryHk2eqLKqsr2St3xhIjhOjQv5wQUBhaTuix4WOSacqj0ONWfKVU12Eug3LEAB95GBkMA==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.30.5.tgz", + "integrity": "sha512-kZ80w/M2AvsbRvOr3PjaNh6qEW1LFqs2pLdo2s5R38B2HYXG8Z0PP48/4+j1QHJFL3ssHIbJ4odPRS8PlHrFfw==", "dev": true }, "@typescript-eslint/typescript-estree": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.30.4.tgz", - "integrity": "sha512-V4VnEs6/J9/nNizaA12IeU4SAeEYaiKr7XndLNfV5+3zZSB4hIu6EhHJixTKhvIqA+EEHgBl6re8pivBMLLO1w==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.30.5.tgz", + "integrity": "sha512-qGTc7QZC801kbYjAr4AgdOfnokpwStqyhSbiQvqGBLixniAKyH+ib2qXIVo4P9NgGzwyfD9I0nlJN7D91E1VpQ==", "dev": true, "requires": { - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/visitor-keys": "5.30.4", + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/visitor-keys": "5.30.5", "debug": "^4.3.4", "globby": "^11.1.0", "is-glob": "^4.0.3", @@ -3034,26 +3034,26 @@ } }, "@typescript-eslint/utils": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.30.4.tgz", - "integrity": "sha512-a+GQrJzOUhn4WT1mUumXDyam+22Oo4c5K/jnZ+6r/4WTQF3q8e4CsC9PLHb4SnOClzOqo/5GLZWvkE1aa5UGKQ==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.30.5.tgz", + "integrity": "sha512-o4SSUH9IkuA7AYIfAvatldovurqTAHrfzPApOZvdUq01hHojZojCFXx06D/aFpKCgWbMPRdJBWAC3sWp3itwTA==", "dev": true, "requires": { "@types/json-schema": "^7.0.9", - "@typescript-eslint/scope-manager": "5.30.4", - "@typescript-eslint/types": "5.30.4", - "@typescript-eslint/typescript-estree": "5.30.4", + "@typescript-eslint/scope-manager": "5.30.5", + "@typescript-eslint/types": "5.30.5", + "@typescript-eslint/typescript-estree": "5.30.5", "eslint-scope": "^5.1.1", "eslint-utils": "^3.0.0" } }, "@typescript-eslint/visitor-keys": { - "version": "5.30.4", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.30.4.tgz", - "integrity": "sha512-ulKGse3mruSc8x6l8ORSc6+1ORyJzKmZeIaRTu/WpaF/jx3vHvEn5XZUKF9XaVg2710mFmTAUlLcLYLPp/Zf/Q==", + "version": "5.30.5", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.30.5.tgz", + "integrity": "sha512-D+xtGo9HUMELzWIUqcQc0p2PO4NyvTrgIOK/VnSH083+8sq0tiLozNRKuLarwHYGRuA6TVBQSuuLwJUDWd3aaA==", "dev": true, "requires": { - "@typescript-eslint/types": "5.30.4", + "@typescript-eslint/types": "5.30.5", "eslint-visitor-keys": "^3.3.0" } }, @@ -3821,9 +3821,9 @@ } }, "globals": { - "version": "13.15.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.15.0.tgz", - "integrity": "sha512-bpzcOlgDhMG070Av0Vy5Owklpv1I6+j96GhUI7Rh7IzDCKLzboflLrrfqMu8NquDbiR4EOQk7XzJwqVJxicxog==", + "version": "13.16.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.16.0.tgz", + "integrity": "sha512-A1lrQfpNF+McdPOnnFqY3kSN0AFTy485bTi1bkLk4mVPODIUEcSfhHgRqA+QdXPksrSTTztYXx37NFV+GpGk3Q==", "dev": true, "requires": { "type-fest": "^0.20.2" diff --git a/package.json b/package.json index d452549..f368dc6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@api.video/nodejs-client", - "version": "2.2.7", + "version": "2.2.8", "description": "api.video nodejs API client", "keywords": [ "api.video", diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 566e2ab..a917a80 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -39,6 +39,8 @@ export default class HttpClient { chunkSize: number; applicationName?: string; applicationVersion?: string; + sdkName?: string; + sdkVersion?: string; }) { this.apiKey = params.apiKey; this.baseUri = params.baseUri; @@ -46,12 +48,15 @@ export default class HttpClient { this.tokenType = 'Bearer'; this.headers = { Accept: 'application/json, */*;q=0.8', - 'AV-Origin-Client': 'nodejs:2.2.7', - ...(params.applicationName + 'AV-Origin-Client': 'nodejs:2.2.8', + ...(params.applicationName && params.applicationVersion ? { - 'AV-Origin-App': `${params.applicationName}${ - params.applicationVersion ? ':' + params.applicationVersion : '' - }`, + 'AV-Origin-App': `${params.applicationName}:${params.applicationVersion}`, + } + : {}), + ...(params.sdkName && params.sdkVersion + ? { + 'AV-Origin-Sdk': `${params.sdkName}:${params.sdkVersion}`, } : {}), }; diff --git a/src/index.ts b/src/index.ts index 8130021..2d73eea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,8 @@ class ApiVideoClient { chunkSize?: number; applicationName?: string; applicationVersion?: string; + sdkName?: string; + sdkVersion?: string; }) { if ( params.chunkSize && @@ -58,27 +60,13 @@ class ApiVideoClient { ); } - if (params.applicationVersion && !params.applicationName) { - throw new Error( - 'applicationName is mandatory when applicationVersion is set.' - ); - } - if ( - params.applicationName && - !/^[\w-]{1,50}$/.test(params.applicationName) - ) { - throw new Error( - "Invalid applicationName value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." - ); - } - if ( - params.applicationVersion && - !/^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$/.test(params.applicationVersion) - ) { - throw new Error( - 'Invalid applicationVersion value. The version should match the xxx[.yyy][.zzz] pattern.' - ); - } + this.validateOrigin( + 'application', + params.applicationName, + params.applicationVersion + ); + + this.validateOrigin('sdk', params.sdkName, params.sdkVersion); this.httpClient = new HttpClient({ ...params, @@ -172,6 +160,30 @@ class ApiVideoClient { public get webhooks(): WebhooksApi { return this._webhooks; } + + 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; diff --git a/test/client.spec.ts b/test/client.spec.ts index 178ccf8..5423a69 100644 --- a/test/client.spec.ts +++ b/test/client.spec.ts @@ -50,9 +50,19 @@ describe('ApiVideoClient', () => { it('should validate the application name value', () => { expect( - () => new ApiVideoClient({ applicationName: 'application name' }) + () => new ApiVideoClient({ applicationName: 'applicationname' }) ).to.throw( - "Invalid applicationName value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." + 'application version is mandatory when application name is set.' + ); + + expect( + () => + new ApiVideoClient({ + applicationName: 'application name', + applicationVersion: '1.0.0', + }) + ).to.throw( + "Invalid application name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." ); expect( @@ -60,17 +70,22 @@ describe('ApiVideoClient', () => { new ApiVideoClient({ applicationName: '012345678901234567890123456789012345678901234567891', + applicationVersion: '1.0.0', }) ).to.throw( - "Invalid applicationName value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." + "Invalid application name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." ); expect( - () => new ApiVideoClient({ applicationName: 'my-great-application1' }) + () => + new ApiVideoClient({ + applicationName: 'my-great-application1', + applicationVersion: '0.1.1', + }) ).not.to.throw(); expect(() => new ApiVideoClient({ applicationVersion: '1.0.0' })).to.throw( - 'applicationName is mandatory when applicationVersion is set.' + 'application name is mandatory when application version is set.' ); expect( @@ -80,7 +95,7 @@ describe('ApiVideoClient', () => { applicationVersion: '1.1234.0', }) ).to.throw( - 'Invalid applicationVersion value. The version should match the xxx[.yyy][.zzz] pattern.' + 'Invalid application version value. The version should match the xxx[.yyy][.zzz] pattern.' ); expect(