Skip to content

Commit 930d96c

Browse files
authored
Merge pull request #7 from freestylejs/refactor/optimize-type-speed
[refactor] optimize type speed
2 parents a79c31d + 6539c54 commit 930d96c

File tree

17 files changed

+38420
-17413
lines changed

17 files changed

+38420
-17413
lines changed

packages/create_freestyle_fetch/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# create-freestyle-fetch
22

3+
## 1.2.1
4+
5+
### Patch Changes
6+
7+
- Handle strictNullChecks option correctly for model generation, handles nullable unions.
8+
39
## 1.2.0
410

511
### Minor Changes

packages/create_freestyle_fetch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-freestyle-fetch",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Generate freestylejs fetch client from OpenAPI spec",
55
"author": "danpacho",
66
"license": "MIT",
Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,65 @@
11
import { z } from 'zod';
22

3-
export const Product = z.object({
3+
// Helper types for schemas
4+
5+
export type ProductModel = {
6+
'id': string;
7+
'name': string;
8+
'productType': string;
9+
'price': number;
10+
};
11+
12+
export type ElectronicsProductModel = ProductModel & {
13+
'specs'?: {
14+
[key: string]: string;
15+
} | undefined;
16+
};
17+
18+
export type ClothingProductModel = ProductModel & {
19+
'size'?: 'S' | 'M' | 'L' | 'XL' | undefined;
20+
'color'?: string | undefined;
21+
};
22+
23+
export type OrderModel = {
24+
'id'?: string | undefined;
25+
'userId'?: string | undefined;
26+
'products'?: ProductModel[] | undefined;
27+
'total'?: number | undefined;
28+
'status'?: 'pending' | 'shipped' | 'delivered' | undefined;
29+
};
30+
31+
export type ErrorModel = {
32+
'code'?: number | undefined;
33+
'message'?: string | undefined;
34+
};
35+
36+
37+
38+
export const Product: z.ZodType<ProductModel> = z.object({
439
'id': z.uuid(),
540
'name': z.string(),
641
'productType': z.string(),
742
'price': z.number().min(0)
843
});
944

10-
export type ProductModel = z.infer<typeof Product>;
11-
12-
export const ElectronicsProduct = Product.and(z.object({
45+
export const ElectronicsProduct: z.ZodType<ElectronicsProductModel> = Product.and(z.object({
1346
'specs': z.record(z.string(), z.string()).optional()
1447
}));
1548

16-
export type ElectronicsProductModel = z.infer<typeof ElectronicsProduct>;
17-
18-
export const ClothingProduct = Product.and(z.object({
49+
export const ClothingProduct: z.ZodType<ClothingProductModel> = Product.and(z.object({
1950
'size': z.enum(['S', 'M', 'L', 'XL']).optional(),
2051
'color': z.string().optional()
2152
}));
2253

23-
export type ClothingProductModel = z.infer<typeof ClothingProduct>;
24-
25-
export const Order = z.object({
54+
export const Order: z.ZodType<OrderModel> = z.object({
2655
'id': z.uuid().optional(),
2756
'userId': z.string().optional(),
2857
'products': z.array(Product).optional(),
2958
'total': z.number().optional(),
3059
'status': z.enum(['pending', 'shipped', 'delivered']).optional()
3160
});
3261

33-
export type OrderModel = z.infer<typeof Order>;
34-
35-
export const Error = z.object({
62+
export const Error: z.ZodType<ErrorModel> = z.object({
3663
'code': z.number().int().optional(),
3764
'message': z.string().optional()
38-
});
39-
40-
export type ErrorModel = z.infer<typeof Error>;
65+
});
Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,73 @@
11
import { z } from 'zod';
22

3-
export const User = z.object({
3+
// Helper types for schemas
4+
5+
export type UserModel = {
6+
'id': string;
7+
'username': string;
8+
'email': string;
9+
'profile'?: {
10+
'fullName'?: string | undefined;
11+
'joinDate'?: string | undefined;
12+
} | undefined;
13+
'legacyId'?: number | undefined;
14+
};
15+
16+
export type ProductInputModel = {
17+
'name': string;
18+
'description'?: string | undefined;
19+
'price': number;
20+
};
21+
22+
export type ProductModel = ProductInputModel & {
23+
'id'?: string | undefined;
24+
'imageUrl'?: string | undefined;
25+
'stock'?: number | undefined;
26+
};
27+
28+
export type PaginatedResponseModel = {
29+
'page'?: number | undefined;
30+
'pageSize'?: number | undefined;
31+
'total'?: number | undefined;
32+
'items'?: any[] | undefined;
33+
};
34+
35+
export type PaginatedProductResponseModel = PaginatedResponseModel;
36+
37+
export type CreditCardModel = {
38+
'methodType': 'card';
39+
'cardNumber': string;
40+
'expiry'?: string | undefined;
41+
'cvv'?: string | undefined;
42+
};
43+
44+
export type PayPalModel = {
45+
'methodType': 'paypal_account';
46+
'email': string;
47+
};
48+
49+
export type PaymentMethodModel = CreditCardModel | PayPalModel;
50+
51+
export type CallbackPayloadModel = {
52+
'orderId'?: string | undefined;
53+
'status'?: 'PROCESSED' | 'FAILED' | undefined;
54+
'detail'?: string | undefined;
55+
};
56+
57+
export type InventoryUpdatePayloadModel = {
58+
'productId'?: string | undefined;
59+
'newStockLevel'?: number | undefined;
60+
'timestamp'?: string | undefined;
61+
};
62+
63+
export type ApiErrorModel = {
64+
'errorCode'?: string | undefined;
65+
'message'?: string | undefined;
66+
};
67+
68+
69+
70+
export const User: z.ZodType<UserModel> = z.object({
471
'id': z.uuid(),
572
'username': z.string().regex(/^[a-zA-Z0-9_-]{3,16}$/),
673
'email': z.email(),
@@ -11,76 +78,54 @@ export const User = z.object({
1178
'legacyId': z.number().int().optional()
1279
});
1380

14-
export type UserModel = z.infer<typeof User>;
15-
16-
export const ProductInput = z.object({
81+
export const ProductInput: z.ZodType<ProductInputModel> = z.object({
1782
'name': z.string(),
1883
'description': z.string().optional(),
1984
'price': z.number().min(0)
2085
});
2186

22-
export type ProductInputModel = z.infer<typeof ProductInput>;
23-
24-
export const Product = ProductInput.and(z.object({
87+
export const Product: z.ZodType<ProductModel> = ProductInput.and(z.object({
2588
'id': z.uuid().optional(),
2689
'imageUrl': z.url().optional(),
2790
'stock': z.number().int().optional()
2891
}));
2992

30-
export type ProductModel = z.infer<typeof Product>;
31-
32-
export const PaginatedResponse = z.object({
93+
export const PaginatedResponse: z.ZodType<PaginatedResponseModel> = z.object({
3394
'page': z.number().int().optional(),
3495
'pageSize': z.number().int().optional(),
3596
'total': z.number().int().optional(),
3697
'items': z.array(z.any()).optional()
3798
});
3899

39-
export type PaginatedResponseModel = z.infer<typeof PaginatedResponse>;
100+
export const PaginatedProductResponse: z.ZodType<PaginatedProductResponseModel> = PaginatedResponse;
40101

41-
export const PaginatedProductResponse = PaginatedResponse;
42-
43-
export type PaginatedProductResponseModel = z.infer<typeof PaginatedProductResponse>;
44-
45-
export const CreditCard = z.object({
102+
export const CreditCard: z.ZodType<CreditCardModel> = z.object({
46103
'methodType': z.enum(['card']),
47104
'cardNumber': z.string(),
48105
'expiry': z.string().optional(),
49106
'cvv': z.string().optional()
50107
});
51108

52-
export type CreditCardModel = z.infer<typeof CreditCard>;
53-
54-
export const PayPal = z.object({
109+
export const PayPal: z.ZodType<PayPalModel> = z.object({
55110
'methodType': z.enum(['paypal_account']),
56111
'email': z.email()
57112
});
58113

59-
export type PayPalModel = z.infer<typeof PayPal>;
114+
export const PaymentMethod: z.ZodType<PaymentMethodModel> = z.discriminatedUnion('methodType', [CreditCard, PayPal]);
60115

61-
export const PaymentMethod = z.discriminatedUnion('methodType', [CreditCard, PayPal]);
62-
63-
export type PaymentMethodModel = z.infer<typeof PaymentMethod>;
64-
65-
export const CallbackPayload = z.object({
116+
export const CallbackPayload: z.ZodType<CallbackPayloadModel> = z.object({
66117
'orderId': z.uuid().optional(),
67118
'status': z.enum(['PROCESSED', 'FAILED']).optional(),
68119
'detail': z.string().optional()
69120
});
70121

71-
export type CallbackPayloadModel = z.infer<typeof CallbackPayload>;
72-
73-
export const InventoryUpdatePayload = z.object({
122+
export const InventoryUpdatePayload: z.ZodType<InventoryUpdatePayloadModel> = z.object({
74123
'productId': z.uuid().optional(),
75124
'newStockLevel': z.number().int().optional(),
76125
'timestamp': z.iso.datetime().optional()
77126
});
78127

79-
export type InventoryUpdatePayloadModel = z.infer<typeof InventoryUpdatePayload>;
80-
81-
export const ApiError = z.object({
128+
export const ApiError: z.ZodType<ApiErrorModel> = z.object({
82129
'errorCode': z.string().optional(),
83130
'message': z.string().optional()
84-
});
85-
86-
export type ApiErrorModel = z.infer<typeof ApiError>;
131+
});
Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
11
import { z } from 'zod';
22

3-
export const HellObject = z.object({
3+
// Helper types for schemas
4+
5+
export type HellObjectModel = {
6+
'required-key': string;
7+
'optional-key'?: string | undefined;
8+
'spaced key': number;
9+
'$special$'?: boolean | undefined;
10+
};
11+
12+
export type DeepNestedModel = {
13+
'level1'?: {
14+
'level2'?: Array<{
15+
'level3'?: 'deep' | undefined;
16+
}> | undefined;
17+
} | undefined;
18+
};
19+
20+
export type IntersectionHellModel = HellObjectModel & {
21+
'extra'?: string | undefined;
22+
};
23+
24+
export type OptionAModel = {
25+
'type': 'A';
26+
'a'?: string | undefined;
27+
};
28+
29+
export type OptionBModel = {
30+
'type': 'B';
31+
'b'?: number | undefined;
32+
};
33+
34+
export type UnionHellModel = OptionAModel | OptionBModel;
35+
36+
37+
38+
export const HellObject: z.ZodType<HellObjectModel> = z.object({
439
'required-key': z.string(),
540
'optional-key': z.string().optional(),
641
'spaced key': z.number(),
742
'$special$': z.boolean().optional()
843
});
944

10-
export type HellObjectModel = z.infer<typeof HellObject>;
11-
12-
export const DeepNested = z.object({
45+
export const DeepNested: z.ZodType<DeepNestedModel> = z.object({
1346
'level1': z.object({
1447
'level2': z.array(z.object({
1548
'level3': z.enum(['deep']).optional()
1649
})).optional()
1750
}).optional()
1851
});
1952

20-
export type DeepNestedModel = z.infer<typeof DeepNested>;
21-
22-
export const IntersectionHell = HellObject.and(z.object({
53+
export const IntersectionHell: z.ZodType<IntersectionHellModel> = HellObject.and(z.object({
2354
'extra': z.string().optional()
2455
}));
2556

26-
export type IntersectionHellModel = z.infer<typeof IntersectionHell>;
27-
28-
export const OptionA = z.object({
57+
export const OptionA: z.ZodType<OptionAModel> = z.object({
2958
'type': z.enum(['A']),
3059
'a': z.string().optional()
3160
});
3261

33-
export type OptionAModel = z.infer<typeof OptionA>;
34-
35-
export const OptionB = z.object({
62+
export const OptionB: z.ZodType<OptionBModel> = z.object({
3663
'type': z.enum(['B']),
3764
'b': z.number().optional()
3865
});
3966

40-
export type OptionBModel = z.infer<typeof OptionB>;
41-
42-
export const UnionHell = z.discriminatedUnion('type', [OptionA, OptionB]);
43-
44-
export type UnionHellModel = z.infer<typeof UnionHell>;
67+
export const UnionHell: z.ZodType<UnionHellModel> = z.discriminatedUnion('type', [OptionA, OptionB]);

0 commit comments

Comments
 (0)