Skip to content

Commit 104be19

Browse files
committed
feat(types): Add ModuleApiEndpoint, ModuleApiParams and ModuleApiResult to exported types
Better typing `AppModuleBody`
1 parent 200e7da commit 104be19

1 file changed

Lines changed: 82 additions & 53 deletions

File tree

packages/types/index.ts

Lines changed: 82 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,47 @@ import type { CreateTransactionParams } from './modules/create_transaction:param
3838
import type { CreateTransactionResponse } from './modules/create_transaction:response';
3939
import type { CheckoutBody } from './modules/@checkout:params';
4040

41-
type ApiEventName = 'orders-new'
41+
export type {
42+
Products,
43+
Categories,
44+
Brands,
45+
Collections,
46+
Grids,
47+
Carts,
48+
Orders,
49+
Customers,
50+
Stores,
51+
Applications,
52+
Authentications,
53+
ProductSet,
54+
CategorySet,
55+
BrandSet,
56+
CollectionSet,
57+
GridSet,
58+
CartSet,
59+
OrderSet,
60+
CustomerSet,
61+
StoreSet,
62+
ApplicationSet,
63+
AuthenticationSet,
64+
Resource,
65+
ResourceId,
66+
ResourceListResult,
67+
SearchItem,
68+
SearchResult,
69+
EventsResult,
70+
ApplyDiscountParams,
71+
ApplyDiscountResponse,
72+
CalculateShippingParams,
73+
CalculateShippingResponse,
74+
ListPaymentsParams,
75+
ListPaymentsResponse,
76+
CreateTransactionParams,
77+
CreateTransactionResponse,
78+
CheckoutBody,
79+
};
80+
81+
export type ApiEventName = 'orders-new'
4282
| 'orders-anyStatusSet'
4383
| 'orders-paid'
4484
| 'orders-readyForShipping'
@@ -54,7 +94,7 @@ type ApiEventName = 'orders-new'
5494
| 'customers-new'
5595
| 'applications-dataSet';
5696

57-
type AppEventsPayload = {
97+
export type AppEventsPayload = {
5898
evName: ApiEventName,
5999
apiEvent: EventsResult<'events/orders'>['result'][0],
60100
apiDoc: Record<string, any>,
@@ -67,20 +107,54 @@ type AppEventsPayload = {
67107
isInternal?: boolean,
68108
};
69109

70-
type AppModuleName = 'apply_discount'
110+
export type AppModuleName = 'apply_discount'
71111
| 'calculate_shipping'
72112
| 'list_payments'
73113
| 'create_transaction';
74114

75-
type AppModuleBody = {
115+
export type AppModuleBody<M extends AppModuleName | undefined = undefined> = {
76116
storeId: number,
77117
module: AppModuleName,
78-
params: {
79-
[key: string]: any,
80-
},
118+
params:
119+
M extends 'list_payments' ? ListPaymentsParams :
120+
M extends 'calculate_shipping' ? CalculateShippingParams :
121+
M extends 'apply_discount' ? ApplyDiscountParams :
122+
M extends 'create_transaction' ? CreateTransactionParams :
123+
Record<string, any>,
81124
application: Applications,
82125
};
83126

127+
export type AppModuleResponse<M extends AppModuleName | undefined = undefined> =
128+
M extends 'list_payments' ? ListPaymentsResponse :
129+
M extends 'calculate_shipping' ? CalculateShippingResponse :
130+
M extends 'apply_discount' ? ApplyDiscountResponse :
131+
M extends 'create_transaction' ? CreateTransactionResponse :
132+
Record<string, any>;
133+
134+
export type ModuleApiEndpoint = Exclude<AppModuleName, 'create_transaction'> | '@checkout';
135+
136+
export type ModuleApiParams<M extends ModuleApiEndpoint> =
137+
M extends 'list_payments' ? ListPaymentsParams :
138+
M extends 'calculate_shipping' ? CalculateShippingParams :
139+
M extends 'apply_discount' ? ApplyDiscountParams :
140+
M extends '@checkout' ? CheckoutBody :
141+
never;
142+
143+
export type ModuleApiResult<M extends ModuleApiEndpoint> = {
144+
result: {
145+
_id: Applications['_id'],
146+
app_id: Applications['app_id'],
147+
took: number,
148+
version: Applications['version'],
149+
validated: boolean,
150+
response_errors: null | string,
151+
error: boolean,
152+
error_message: null | string,
153+
response: M extends AppModuleName ? AppModuleResponse<M> : Record<string, any>,
154+
}[],
155+
meta: ModuleApiParams<M>,
156+
};
157+
84158
type PaymentMethodFlag = 'pix'
85159
| 'visa'
86160
| 'mastercard'
@@ -91,7 +165,7 @@ type PaymentMethodFlag = 'pix'
91165
| 'diners'
92166
| 'discover';
93167

94-
type SettingsContent = {
168+
export type SettingsContent = {
95169
domain: string,
96170
name: string,
97171
description: string,
@@ -147,48 +221,3 @@ type SettingsContent = {
147221
favoritesUrl?: string,
148222
metafields?: Record<string, any>,
149223
};
150-
151-
export type {
152-
Products,
153-
Categories,
154-
Brands,
155-
Collections,
156-
Grids,
157-
Carts,
158-
Orders,
159-
Customers,
160-
Stores,
161-
Applications,
162-
Authentications,
163-
ProductSet,
164-
CategorySet,
165-
BrandSet,
166-
CollectionSet,
167-
GridSet,
168-
CartSet,
169-
OrderSet,
170-
CustomerSet,
171-
StoreSet,
172-
ApplicationSet,
173-
AuthenticationSet,
174-
Resource,
175-
ResourceId,
176-
ResourceListResult,
177-
SearchItem,
178-
SearchResult,
179-
EventsResult,
180-
ApiEventName,
181-
AppEventsPayload,
182-
AppModuleName,
183-
AppModuleBody,
184-
ApplyDiscountParams,
185-
ApplyDiscountResponse,
186-
CalculateShippingParams,
187-
CalculateShippingResponse,
188-
ListPaymentsParams,
189-
ListPaymentsResponse,
190-
CreateTransactionParams,
191-
CreateTransactionResponse,
192-
CheckoutBody,
193-
SettingsContent,
194-
};

0 commit comments

Comments
 (0)