Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/lib/api/CommercetoolsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ import type {
ProductSelectionUpdateAction,
ProductsInStorePagedQueryResponse,
ProductType,
ProductTypeDraft,
ProductTypePagedQueryResponse,
ProductTypeUpdate,
ProductUpdate,
ReplicaCartDraft,
ShippingMethod,
Expand Down Expand Up @@ -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<ProductType> {
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<ProductType> {
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<ProductType> {
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
Expand Down
59 changes: 59 additions & 0 deletions src/test/api/CommercetoolsApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down