Skip to content

Commit 3d61678

Browse files
authored
chore(ts-sdk-client): improve type definitions (#1066)
- improve the process function type definitions
1 parent eaaad59 commit 3d61678

5 files changed

Lines changed: 42 additions & 39 deletions

File tree

.changeset/nice-dryers-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/ts-client': patch
3+
---
4+
5+
Improve typescript types definitions

packages/sdk-client-v3/src/client/client.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Client,
1010
ClientOptions,
1111
ClientRequest,
12+
ClientResponse,
1213
ClientResult,
1314
Dispatch,
1415
Middleware,
@@ -35,11 +36,11 @@ function compose({
3536

3637
// process batch requests
3738
let _options: ClientOptions
38-
export function process(
39+
export function process<T extends object = any>(
3940
request: ClientRequest,
40-
fn: ProcessFn,
41+
fn: ProcessFn<T>,
4142
processOpt?: ProcessOptions
42-
): Promise<Array<ClientRequest>> {
43+
): Promise<Array<T>> {
4344
validate('process', request, { allowedMethods: ['GET'] })
4445

4546
if (typeof fn !== 'function')
@@ -90,8 +91,9 @@ export function process(
9091
}
9192

9293
try {
93-
const payload: ClientResult =
94-
await createClient(_options).execute(enhancedRequest)
94+
const payload: ClientResult = await createClient(_options).execute(
95+
enhancedRequest
96+
)
9597

9698
const { results, count: resultsLength } = payload?.body || {}
9799

packages/sdk-client-v3/src/types/types.d.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ export type HttpErrorType = {
2525
statusCode: number
2626
originalRequest?: ClientRequest
2727
/**
28-
* @deprecated
28+
* @deprecated use `error` instead
2929
*/
3030
body?: JsonObject
31+
error?: JsonObject
3132
retryCount?: number
3233
headers?: Record<string, any>
3334
[key: string]: any
@@ -83,7 +84,7 @@ export type ClientResponse<T = any> = {
8384
retryCount?: number
8485
}
8586

86-
export type ClientResult = ClientResponse
87+
export type ClientResult<T extends object = any> = ClientResponse<T>
8788
export type ClientOptions = { middlewares: Array<Middleware> }
8889

8990
export type Credentials = {
@@ -322,15 +323,20 @@ type TResponse = {
322323

323324
export type Client = {
324325
execute(request: ClientRequest): Promise<ClientResult>
325-
process: (
326+
process<T extends object = any>(
326327
request: ClientRequest,
327-
fn: ProcessFn,
328+
fn: ProcessFn<T>,
328329
processOpt?: ProcessOptions
329-
) => Promise<unknown>
330+
): Promise<Array<T> | Array<void>>
330331
}
331332

332-
export type ProcessFn = (result: ClientResult) => Promise<unknown>
333-
export type ProcessOptions = { accumulate?: boolean; total?: number }
333+
export type ProcessFn<T extends object> = (result: ClientResult<T>) => Promise<ClientResult<T>>
334+
export type ProcessOptions = {
335+
limit?: number;
336+
sort?: string;
337+
accumulate?: boolean;
338+
total?: number;
339+
}
334340

335341
export type ErrorHandlerOptions = {
336342
error: HttpErrorType
@@ -343,9 +349,9 @@ export type ErrorMiddlewareOptions = {
343349
handler?: (args: ErrorHandlerOptions) => Promise<MiddlewareResponse>
344350
}
345351

346-
export type SuccessResult = {
352+
export type SuccessResult<T extends Record<string | number, Record<string, any>>> = {
347353
body: {
348-
results: Record<string | number, Record<string, any>>;
354+
results: Array<T>;
349355
count: number;
350356
};
351357
statusCode: number;

packages/sdk-client-v3/tests/client.test/client.test.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import {
44
createClient,
55
MiddlewareRequest,
66
MiddlewareResponse,
7-
HttpErrorType,
87
Process,
98
Client,
10-
MethodType,
119
ClientBuilder,
1210
} from '../../src'
1311
import { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk'
@@ -302,7 +300,7 @@ describe('process', () => {
302300
})
303301

304302
return client
305-
.process(request, () => Promise.resolve('OK'))
303+
.process(request, () => Promise.resolve('OK' as any))
306304
.then((response) => {
307305
expect(response).toEqual(['OK', 'OK', 'OK'])
308306
})
@@ -360,7 +358,7 @@ describe('process', () => {
360358
})
361359

362360
return client
363-
.process(request, () => Promise.resolve('OK'), { total: 46 })
361+
.process(request, () => Promise.resolve('OK' as any), { total: 46 })
364362
.then((response) => {
365363
expect(response).toEqual(['OK', 'OK', 'OK'])
366364
})
@@ -418,7 +416,7 @@ describe('process', () => {
418416
limit: 5,
419417
})}`,
420418
},
421-
() => Promise.resolve('OK')
419+
() => Promise.resolve('OK') as any
422420
)
423421
})
424422

@@ -529,7 +527,7 @@ describe('process - exposed', () => {
529527
],
530528
})
531529

532-
return Process(request, () => Promise.resolve('OK'), {}).then(
530+
return Process(request, () => Promise.resolve('OK') as any, {}).then(
533531
(response) => {
534532
expect(response).toEqual(['OK', 'OK', 'OK'])
535533
}
@@ -582,11 +580,11 @@ describe('process - exposed', () => {
582580
],
583581
})
584582

585-
return Process(request, () => Promise.resolve('OK'), { total: 46 }).then(
586-
(response) => {
587-
expect(response).toEqual(['OK', 'OK', 'OK'])
588-
}
589-
)
583+
return Process(request, () => Promise.resolve('OK') as any, {
584+
total: 46,
585+
}).then((response) => {
586+
expect(response).toEqual(['OK', 'OK', 'OK'])
587+
})
590588
})
591589

592590
test('process and resolve pagination by preserving original query', () => {
@@ -635,7 +633,7 @@ describe('process - exposed', () => {
635633
limit: 5,
636634
})}`,
637635
},
638-
() => Promise.resolve('OK'),
636+
() => Promise.resolve('OK') as any,
639637
{}
640638
)
641639
})
@@ -688,7 +686,7 @@ describe('process - exposed', () => {
688686
})
689687

690688
let fnCall = 0
691-
const processRes = await Process(
689+
const processRes = await Process<{ results: [] }>(
692690
{
693691
...request,
694692
uri: `${request.uri}?${stringifyURLString({
@@ -698,11 +696,11 @@ describe('process - exposed', () => {
698696
})}`,
699697
},
700698
(res) => {
701-
expect(res.body.results).toEqual(reqStubs[fnCall].body.results)
699+
expect(res.body?.results).toEqual(reqStubs[fnCall].body.results)
702700
expect(fnCall).toBeLessThan(2) // should not call fn if the last page is empty
703701

704702
fnCall += 1
705-
return Promise.resolve(`OK${fnCall}`)
703+
return Promise.resolve(`OK${fnCall}`) as any
706704
},
707705
{
708706
accumulate: true,
@@ -713,7 +711,7 @@ describe('process - exposed', () => {
713711
expect(fnCall).toBe(2) // fn was called two times
714712
})
715713

716-
test('process and reject on rejection from user', () => {
714+
test('process and reject on rejection from user', async () => {
717715
createClient({
718716
middlewares: [
719717
(next) =>

tsconfig.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,5 @@
1010
"resolveJsonModule": true,
1111
"downlevelIteration": true
1212
},
13-
"exclude": ["./examples/"]
14-
// "references": [
15-
// {
16-
// "path": "./packages/sdk-client"
17-
// },
18-
// {
19-
// "path": "./packages/sdk-client-v3"
20-
// }
21-
// ]
13+
"include": ["packages"]
2214
}

0 commit comments

Comments
 (0)