diff --git a/package.json b/package.json index 8a7ec8c04..699005512 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ }, "homepage": "https://github.com/gradientedge/commercetools-utils#readme", "devDependencies": { + "@jest/globals": "^29.7.0", "@sinonjs/fake-timers": "11.2.2", "@swc/core": "1.5.7", "@swc/helpers": "0.5.11", @@ -99,5 +100,6 @@ "commitizen": { "path": "./node_modules/cz-conventional-changelog" } - } + }, + "packageManager": "pnpm@9.9.0+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9722fe3c8..54ac1340f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,6 +24,9 @@ importers: specifier: 0.6.9 version: 0.6.9 devDependencies: + '@jest/globals': + specifier: ^29.7.0 + version: 29.7.0 '@sinonjs/fake-timers': specifier: 11.2.2 version: 11.2.2 @@ -35,7 +38,7 @@ importers: version: 0.5.11 '@swc/jest': specifier: 0.2.36 - version: 0.2.36(@swc/core@1.5.7) + version: 0.2.36(@swc/core@1.5.7(@swc/helpers@0.5.11)) '@types/jest': specifier: 29.5.12 version: 29.5.12 @@ -62,7 +65,7 @@ importers: version: 0.6.36 '@typescript-eslint/eslint-plugin': specifier: 7.10.0 - version: 7.10.0(@typescript-eslint/parser@7.10.0)(eslint@8.56.0)(typescript@5.4.5) + version: 7.10.0(@typescript-eslint/parser@7.10.0(eslint@8.56.0)(typescript@5.4.5))(eslint@8.56.0)(typescript@5.4.5) '@typescript-eslint/parser': specifier: 7.10.0 version: 7.10.0(eslint@8.56.0)(typescript@5.4.5) @@ -86,10 +89,10 @@ importers: version: 9.0.11 jest: specifier: 29.7.0 - version: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + version: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) jest-extended: specifier: ^4.0.2 - version: 4.0.2(jest@29.7.0) + version: 4.0.2(jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5))) jest-matcher-specific-error: specifier: 1.0.0 version: 1.0.0 @@ -110,10 +113,10 @@ importers: version: 23.1.1(typescript@5.4.5) ts-jest: specifier: 29.1.3 - version: 29.1.3(@babel/core@7.24.6)(jest@29.7.0)(typescript@5.4.5) + version: 29.1.3(@babel/core@7.24.6)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.24.6))(jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)))(typescript@5.4.5) ts-node: specifier: ^10.9.2 - version: 10.9.2(@swc/core@1.5.7)(@types/node@20.12.12)(typescript@5.4.5) + version: 10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5) typedoc: specifier: 0.25.13 version: 0.25.13(typescript@5.4.5) @@ -3619,7 +3622,7 @@ snapshots: '@commitlint/types': 19.0.3 chalk: 5.3.0 cosmiconfig: 9.0.0(typescript@5.4.5) - cosmiconfig-typescript-loader: 5.0.0(@types/node@20.12.12)(cosmiconfig@9.0.0)(typescript@5.4.5) + cosmiconfig-typescript-loader: 5.0.0(@types/node@20.12.12)(cosmiconfig@9.0.0(typescript@5.4.5))(typescript@5.4.5) lodash.isplainobject: 4.0.6 lodash.merge: 4.6.2 lodash.uniq: 4.5.0 @@ -3711,7 +3714,7 @@ snapshots: jest-util: 29.7.0 slash: 3.0.0 - '@jest/core@29.7.0(ts-node@10.9.2)': + '@jest/core@29.7.0(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5))': dependencies: '@jest/console': 29.7.0 '@jest/reporters': 29.7.0 @@ -3725,7 +3728,7 @@ snapshots: exit: 0.1.2 graceful-fs: 4.2.11 jest-changed-files: 29.7.0 - jest-config: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + jest-config: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) jest-haste-map: 29.7.0 jest-message-util: 29.7.0 jest-regex-util: 29.6.3 @@ -3977,7 +3980,7 @@ snapshots: '@sec-ant/readable-stream@0.4.1': {} - '@semantic-release/commit-analyzer@12.0.0(semantic-release@23.1.1)': + '@semantic-release/commit-analyzer@12.0.0(semantic-release@23.1.1(typescript@5.4.5))': dependencies: conventional-changelog-angular: 7.0.0 conventional-commits-filter: 4.0.0 @@ -3992,7 +3995,7 @@ snapshots: '@semantic-release/error@4.0.0': {} - '@semantic-release/github@10.0.5(semantic-release@23.1.1)': + '@semantic-release/github@10.0.5(semantic-release@23.1.1(typescript@5.4.5))': dependencies: '@octokit/core': 6.1.2 '@octokit/plugin-paginate-rest': 11.3.0(@octokit/core@6.1.2) @@ -4014,7 +4017,7 @@ snapshots: transitivePeerDependencies: - supports-color - '@semantic-release/npm@12.0.1(semantic-release@23.1.1)': + '@semantic-release/npm@12.0.1(semantic-release@23.1.1(typescript@5.4.5))': dependencies: '@semantic-release/error': 4.0.0 aggregate-error: 5.0.0 @@ -4031,7 +4034,7 @@ snapshots: semver: 7.6.2 tempy: 3.1.0 - '@semantic-release/release-notes-generator@13.0.0(semantic-release@23.1.1)': + '@semantic-release/release-notes-generator@13.0.0(semantic-release@23.1.1(typescript@5.4.5))': dependencies: conventional-changelog-angular: 7.0.0 conventional-changelog-writer: 7.0.1 @@ -4100,7 +4103,6 @@ snapshots: '@swc/core@1.5.7(@swc/helpers@0.5.11)': dependencies: '@swc/counter': 0.1.3 - '@swc/helpers': 0.5.11 '@swc/types': 0.1.7 optionalDependencies: '@swc/core-darwin-arm64': 1.5.7 @@ -4113,6 +4115,7 @@ snapshots: '@swc/core-win32-arm64-msvc': 1.5.7 '@swc/core-win32-ia32-msvc': 1.5.7 '@swc/core-win32-x64-msvc': 1.5.7 + '@swc/helpers': 0.5.11 '@swc/counter@0.1.3': {} @@ -4120,7 +4123,7 @@ snapshots: dependencies: tslib: 2.6.2 - '@swc/jest@0.2.36(@swc/core@1.5.7)': + '@swc/jest@0.2.36(@swc/core@1.5.7(@swc/helpers@0.5.11))': dependencies: '@jest/create-cache-key-function': 29.7.0 '@swc/core': 1.5.7(@swc/helpers@0.5.11) @@ -4216,7 +4219,7 @@ snapshots: dependencies: '@types/yargs-parser': 21.0.3 - '@typescript-eslint/eslint-plugin@7.10.0(@typescript-eslint/parser@7.10.0)(eslint@8.56.0)(typescript@5.4.5)': + '@typescript-eslint/eslint-plugin@7.10.0(@typescript-eslint/parser@7.10.0(eslint@8.56.0)(typescript@5.4.5))(eslint@8.56.0)(typescript@5.4.5)': dependencies: '@eslint-community/regexpp': 4.10.0 '@typescript-eslint/parser': 7.10.0(eslint@8.56.0)(typescript@5.4.5) @@ -4229,6 +4232,7 @@ snapshots: ignore: 5.3.1 natural-compare: 1.4.0 ts-api-utils: 1.3.0(typescript@5.4.5) + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -4241,6 +4245,7 @@ snapshots: '@typescript-eslint/visitor-keys': 7.10.0 debug: 4.3.4 eslint: 8.56.0 + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -4257,6 +4262,7 @@ snapshots: debug: 4.3.4 eslint: 8.56.0 ts-api-utils: 1.3.0(typescript@5.4.5) + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -4273,6 +4279,7 @@ snapshots: minimatch: 9.0.4 semver: 7.6.2 ts-api-utils: 1.3.0(typescript@5.4.5) + optionalDependencies: typescript: 5.4.5 transitivePeerDependencies: - supports-color @@ -4695,7 +4702,7 @@ snapshots: core-util-is@1.0.3: {} - cosmiconfig-typescript-loader@5.0.0(@types/node@20.12.12)(cosmiconfig@9.0.0)(typescript@5.4.5): + cosmiconfig-typescript-loader@5.0.0(@types/node@20.12.12)(cosmiconfig@9.0.0(typescript@5.4.5))(typescript@5.4.5): dependencies: '@types/node': 20.12.12 cosmiconfig: 9.0.0(typescript@5.4.5) @@ -4709,15 +4716,16 @@ snapshots: import-fresh: 3.3.0 js-yaml: 4.1.0 parse-json: 5.2.0 + optionalDependencies: typescript: 5.4.5 - create-jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2): + create-jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)): dependencies: '@jest/types': 29.6.3 chalk: 4.1.2 exit: 0.1.2 graceful-fs: 4.2.11 - jest-config: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + jest-config: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) jest-util: 29.7.0 prompts: 2.4.2 transitivePeerDependencies: @@ -5686,16 +5694,16 @@ snapshots: - babel-plugin-macros - supports-color - jest-cli@29.7.0(@types/node@20.12.12)(ts-node@10.9.2): + jest-cli@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)): dependencies: - '@jest/core': 29.7.0(ts-node@10.9.2) + '@jest/core': 29.7.0(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) '@jest/test-result': 29.7.0 '@jest/types': 29.6.3 chalk: 4.1.2 - create-jest: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + create-jest: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) exit: 0.1.2 import-local: 3.1.0 - jest-config: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + jest-config: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) jest-util: 29.7.0 jest-validate: 29.7.0 yargs: 17.7.2 @@ -5705,12 +5713,11 @@ snapshots: - supports-color - ts-node - jest-config@29.7.0(@types/node@20.12.12)(ts-node@10.9.2): + jest-config@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)): dependencies: '@babel/core': 7.24.6 '@jest/test-sequencer': 29.7.0 '@jest/types': 29.6.3 - '@types/node': 20.12.12 babel-jest: 29.7.0(@babel/core@7.24.6) chalk: 4.1.2 ci-info: 3.9.0 @@ -5730,7 +5737,9 @@ snapshots: pretty-format: 29.7.0 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.9.2(@swc/core@1.5.7)(@types/node@20.12.12)(typescript@5.4.5) + optionalDependencies: + '@types/node': 20.12.12 + ts-node: 10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5) transitivePeerDependencies: - babel-plugin-macros - supports-color @@ -5763,11 +5772,12 @@ snapshots: jest-mock: 29.7.0 jest-util: 29.7.0 - jest-extended@4.0.2(jest@29.7.0): + jest-extended@4.0.2(jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5))): dependencies: - jest: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) jest-diff: 29.7.0 jest-get-type: 29.6.3 + optionalDependencies: + jest: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) jest-get-type@29.6.3: {} @@ -5820,7 +5830,7 @@ snapshots: jest-util: 29.7.0 jest-pnp-resolver@1.2.3(jest-resolve@29.7.0): - dependencies: + optionalDependencies: jest-resolve: 29.7.0 jest-regex-util@29.6.3: {} @@ -5958,12 +5968,12 @@ snapshots: merge-stream: 2.0.0 supports-color: 8.1.1 - jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2): + jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)): dependencies: - '@jest/core': 29.7.0(ts-node@10.9.2) + '@jest/core': 29.7.0(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) '@jest/types': 29.6.3 import-local: 3.1.0 - jest-cli: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + jest-cli: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) transitivePeerDependencies: - '@types/node' - babel-plugin-macros @@ -6541,11 +6551,11 @@ snapshots: semantic-release@23.1.1(typescript@5.4.5): dependencies: - '@semantic-release/commit-analyzer': 12.0.0(semantic-release@23.1.1) + '@semantic-release/commit-analyzer': 12.0.0(semantic-release@23.1.1(typescript@5.4.5)) '@semantic-release/error': 4.0.0 - '@semantic-release/github': 10.0.5(semantic-release@23.1.1) - '@semantic-release/npm': 12.0.1(semantic-release@23.1.1) - '@semantic-release/release-notes-generator': 13.0.0(semantic-release@23.1.1) + '@semantic-release/github': 10.0.5(semantic-release@23.1.1(typescript@5.4.5)) + '@semantic-release/npm': 12.0.1(semantic-release@23.1.1(typescript@5.4.5)) + '@semantic-release/release-notes-generator': 13.0.0(semantic-release@23.1.1(typescript@5.4.5)) aggregate-error: 5.0.0 cosmiconfig: 9.0.0(typescript@5.4.5) debug: 4.3.4 @@ -6849,12 +6859,11 @@ snapshots: dependencies: typescript: 5.4.5 - ts-jest@29.1.3(@babel/core@7.24.6)(jest@29.7.0)(typescript@5.4.5): + ts-jest@29.1.3(@babel/core@7.24.6)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.24.6))(jest@29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)))(typescript@5.4.5): dependencies: - '@babel/core': 7.24.6 bs-logger: 0.2.6 fast-json-stable-stringify: 2.1.0 - jest: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2) + jest: 29.7.0(@types/node@20.12.12)(ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5)) jest-util: 29.7.0 json5: 2.2.3 lodash.memoize: 4.1.2 @@ -6862,11 +6871,15 @@ snapshots: semver: 7.6.2 typescript: 5.4.5 yargs-parser: 21.1.1 + optionalDependencies: + '@babel/core': 7.24.6 + '@jest/transform': 29.7.0 + '@jest/types': 29.6.3 + babel-jest: 29.7.0(@babel/core@7.24.6) - ts-node@10.9.2(@swc/core@1.5.7)(@types/node@20.12.12)(typescript@5.4.5): + ts-node@10.9.2(@swc/core@1.5.7(@swc/helpers@0.5.11))(@types/node@20.12.12)(typescript@5.4.5): dependencies: '@cspotcode/source-map-support': 0.8.1 - '@swc/core': 1.5.7(@swc/helpers@0.5.11) '@tsconfig/node10': 1.0.11 '@tsconfig/node12': 1.0.11 '@tsconfig/node14': 1.0.3 @@ -6881,6 +6894,8 @@ snapshots: typescript: 5.4.5 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 + optionalDependencies: + '@swc/core': 1.5.7(@swc/helpers@0.5.11) tslib@2.6.2: {} diff --git a/src/lib/api/CommercetoolsApi.ts b/src/lib/api/CommercetoolsApi.ts index f39c4708a..02aebc922 100644 --- a/src/lib/api/CommercetoolsApi.ts +++ b/src/lib/api/CommercetoolsApi.ts @@ -4,6 +4,7 @@ import { OrderFromCartDraft, PaymentDraft, ProductTypePagedQueryResponse, + TaxCategory, TypePagedQueryResponse, } from '../index.js' import { CommercetoolsError } from '../error/index.js' @@ -2542,6 +2543,33 @@ export class CommercetoolsApi { }) } + /** + * Get a tax category given its id + * https://docs.commercetools.com/api/projects/taxCategories#get-taxcategory-by-id + */ + getTaxCategoryById(options: CommonRequestOptions & { id: string }): Promise { + ensureNonEmptyString({ value: options.id, name: 'id' }) + + return this.request({ + ...this.extractCommonRequestOptions(options), + path: `/tax-categories/${encodeURIComponent(options.id)}`, + method: 'GET', + }) + } + + /** + * Get a tax category given its key + * https://docs.commercetools.com/api/projects/taxCategories#get-taxcategory-by-key + */ + getTaxCategoryByKey(options: CommonRequestOptions & { key: string }): Promise { + ensureNonEmptyString({ value: options.key, name: 'key' }) + return this.request({ + ...this.extractCommonRequestOptions(options), + path: `/tax-categories/key=${encodeURIComponent(options.key)}`, + method: 'GET', + }) + } + /** * Query tax categories * https://docs.commercetools.com/api/projects/taxCategories#query-taxcategories diff --git a/src/test/api/CommercetoolsApi.test.ts b/src/test/api/CommercetoolsApi.test.ts index 49a6f0465..be7b2f8f7 100644 --- a/src/test/api/CommercetoolsApi.test.ts +++ b/src/test/api/CommercetoolsApi.test.ts @@ -3741,6 +3741,71 @@ describe('CommercetoolsApi', () => { }) }) + describe('Tax categories', () => { + describe('getTaxCategoryById', () => { + it('should make a GET request to the correct endpoint', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .get('/test-project-key/tax-categories/my-tax-category-id') + .reply(200, { success: true }) + const api = new CommercetoolsApi(defaultConfig) + + const product = await api.getTaxCategoryById({ id: 'my-tax-category-id' }) + + expect(product).toEqual({ success: true }) + }) + + it('should throw an error when an empty ID is being passed as a parameter', async () => { + const api = new CommercetoolsApi(defaultConfig) + + await expect(() => api.getTaxCategoryById({ id: ' ' })).toThrow(`The string parameter 'id' cannot be empty`) + }) + }) + + describe('getTaxCategoryByKey', () => { + it('should make a GET request to the correct endpoint', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .get('/test-project-key/tax-categories/key=my-tax-category-key') + .reply(200, { success: true }) + const api = new CommercetoolsApi(defaultConfig) + + const product = await api.getTaxCategoryByKey({ key: 'my-tax-category-key' }) + + expect(product).toEqual({ success: true }) + }) + }) + + describe('queryTaxCategories', () => { + it('should make a GET request to the correct endpoint when no parameters are passed', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .get('/test-project-key/tax-categories') + .reply(200, { success: true }) + const api = new CommercetoolsApi(defaultConfig) + + const taxCategoriesQueryResult = await api.queryTaxCategories() + + expect(taxCategoriesQueryResult).toEqual({ success: true }) + }) + + it('should make a GET request to the correct endpoint with the passed in parameters in the querystring', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .get('/test-project-key/tax-categories') + .query({ + where: 'key=123', + }) + .reply(200, singleItemResponse) + const api = new CommercetoolsApi(defaultConfig) + + const taxCategoriesQueryResult = await api.queryTaxCategories({ + params: { + where: 'key=123', + }, + }) + + expect(taxCategoriesQueryResult).toEqual(singleItemResponse) + }) + }) + }) + describe('graphql', () => { it('should call the /graphql endpoint with the data passed in, using the client access token', async () => { nock('https://api.europe-west1.gcp.commercetools.com', { @@ -3910,35 +3975,4 @@ describe('CommercetoolsApi', () => { ).not.toThrowError() }) }) - - describe('queryTaxCaterories', () => { - it('should make a GET request to the correct endpoint when no parameters are passed', async () => { - nock('https://api.europe-west1.gcp.commercetools.com') - .get('/test-project-key/tax-categories') - .reply(200, { success: true }) - const api = new CommercetoolsApi(defaultConfig) - - const taxCategoriesQueryResult = await api.queryTaxCategories() - - expect(taxCategoriesQueryResult).toEqual({ success: true }) - }) - - it('should make a GET request to the correct endpoint with the passed in parameters in the querystring', async () => { - nock('https://api.europe-west1.gcp.commercetools.com') - .get('/test-project-key/tax-categories') - .query({ - where: 'key=123', - }) - .reply(200, singleItemResponse) - const api = new CommercetoolsApi(defaultConfig) - - const taxCategoriesQueryResult = await api.queryTaxCategories({ - params: { - where: 'key=123', - }, - }) - - expect(taxCategoriesQueryResult).toEqual(singleItemResponse) - }) - }) })