diff --git a/src/lib/api/CommercetoolsApi.ts b/src/lib/api/CommercetoolsApi.ts index ec1058994a..7a65241037 100644 --- a/src/lib/api/CommercetoolsApi.ts +++ b/src/lib/api/CommercetoolsApi.ts @@ -92,7 +92,9 @@ import type { ProductSelectionUpdateAction, ProductsInStorePagedQueryResponse, ProductType, + ProductTypeDraft, ProductTypePagedQueryResponse, + ProductTypeUpdate, ProductUpdate, ReplicaCartDraft, ShippingMethod, @@ -2146,6 +2148,51 @@ export class CommercetoolsApi { }) } + /** + * Create a product type: + * https://docs.commercetools.com/api/projects/productTypes#create-producttype + */ + createProductType(options: CommonRequestOptions & { data: ProductTypeDraft }): Promise { + return this.request({ + ...this.extractCommonRequestOptions(options), + path: `/product-types`, + method: 'POST', + data: options.data, + }) + } + + /** + * Update a product type by id: + * https://docs.commercetools.com/api/projects/productTypes#update-producttype-by-id + */ + updateProductTypeById(options: CommonRequestOptions & { id: string; data: ProductTypeUpdate }): Promise { + ensureNonEmptyString({ value: options.id, name: 'id' }) + + return this.request({ + ...this.extractCommonRequestOptions(options), + path: `/product-types/${encodeURIComponent(options.id)}`, + method: 'POST', + data: options.data, + }) + } + + /** + * Update a product type by key: + * https://docs.commercetools.com/api/projects/productTypes#update-producttype-by-key + */ + updateProductTypeByKey( + options: CommonRequestOptions & { key: string; data: ProductTypeUpdate }, + ): Promise { + ensureNonEmptyString({ value: options.key, name: 'key' }) + + return this.request({ + ...this.extractCommonRequestOptions(options), + path: `/product-types/key=${encodeURIComponent(options.key)}`, + method: 'POST', + data: options.data, + }) + } + /** * Delete a product type by id * https://docs.commercetools.com/api/projects/productTypes#delete-producttype-by-id diff --git a/src/test/api/CommercetoolsApi.test.ts b/src/test/api/CommercetoolsApi.test.ts index 3c40fbe9b1..ac3f06259c 100644 --- a/src/test/api/CommercetoolsApi.test.ts +++ b/src/test/api/CommercetoolsApi.test.ts @@ -3316,6 +3316,65 @@ describe('CommercetoolsApi', () => { expect(order).toEqual({ success: true }) }) }) + + describe('createProductType', () => { + const productTypeDraft = { key: 'my-product-type-key', name: 'My Product Type', description: 'desc' } as any + + it('should make a POST request to the correct endpoint with the provided data', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .post('/test-project-key/product-types', productTypeDraft) + .reply(200, { success: true }) + const api = new CommercetoolsApi(defaultConfig) + + const result = await api.createProductType({ data: productTypeDraft }) + + expect(result).toEqual({ success: true }) + }) + }) + + describe('updateProductTypeById', () => { + const updateData = { version: 1, actions: [{ action: 'setKey', key: 'new-key' }] } as any + + it('should make a POST request to the correct endpoint with the provided data', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .post('/test-project-key/product-types/my-product-type-id', updateData) + .reply(200, { success: true }) + const api = new CommercetoolsApi(defaultConfig) + + const result = await api.updateProductTypeById({ id: 'my-product-type-id', data: updateData }) + + expect(result).toEqual({ success: true }) + }) + + it('should throw an error when an empty id is passed', async () => { + const api = new CommercetoolsApi(defaultConfig) + await expect(() => api.updateProductTypeById({ id: ' ', data: updateData })).toThrow( + "The string parameter 'id' cannot be empty", + ) + }) + }) + + describe('updateProductTypeByKey', () => { + const updateData = { version: 1, actions: [{ action: 'setKey', key: 'new-key' }] } as any + + it('should make a POST request to the correct endpoint with the provided data', async () => { + nock('https://api.europe-west1.gcp.commercetools.com') + .post('/test-project-key/product-types/key=my-product-type-key', updateData) + .reply(200, { success: true }) + const api = new CommercetoolsApi(defaultConfig) + + const result = await api.updateProductTypeByKey({ key: 'my-product-type-key', data: updateData }) + + expect(result).toEqual({ success: true }) + }) + + it('should throw an error when an empty key is passed', async () => { + const api = new CommercetoolsApi(defaultConfig) + await expect(() => api.updateProductTypeByKey({ key: ' ', data: updateData })).toThrow( + "The string parameter 'key' cannot be empty", + ) + }) + }) }) describe('Types', () => {