exnet-routing 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ import { z } from "zod";
2
+ import { index } from "../index";
3
+ type ApiService = typeof index;
4
+ export type IHttpMethod = "GET" | "POST" | "PUT" | "DELETE";
5
+ export type IHttpMethodWithBody = "POST" | "PUT" | "DELETE";
6
+ export type IHttpMethodWithoutBody = "GET";
7
+ export declare const response: <R extends z.ZodObject<any> | z.ZodArray<any> | z.ZodNull>(data: R) => z.ZodUnion<[z.ZodObject<{
8
+ data: R;
9
+ message: z.ZodOptional<z.ZodString>;
10
+ status: z.ZodOptional<z.ZodNumber>;
11
+ error: z.ZodOptional<z.ZodNever>;
12
+ success: z.ZodLiteral<true>;
13
+ }, "strip", z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<{
14
+ data: R;
15
+ message: z.ZodOptional<z.ZodString>;
16
+ status: z.ZodOptional<z.ZodNumber>;
17
+ error: z.ZodOptional<z.ZodNever>;
18
+ success: z.ZodLiteral<true>;
19
+ }>, any> extends infer T ? { [k in keyof T]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<{
20
+ data: R;
21
+ message: z.ZodOptional<z.ZodString>;
22
+ status: z.ZodOptional<z.ZodNumber>;
23
+ error: z.ZodOptional<z.ZodNever>;
24
+ success: z.ZodLiteral<true>;
25
+ }>, any>[k]; } : never, z.baseObjectInputType<{
26
+ data: R;
27
+ message: z.ZodOptional<z.ZodString>;
28
+ status: z.ZodOptional<z.ZodNumber>;
29
+ error: z.ZodOptional<z.ZodNever>;
30
+ success: z.ZodLiteral<true>;
31
+ }> extends infer T_1 ? { [k_1 in keyof T_1]: z.baseObjectInputType<{
32
+ data: R;
33
+ message: z.ZodOptional<z.ZodString>;
34
+ status: z.ZodOptional<z.ZodNumber>;
35
+ error: z.ZodOptional<z.ZodNever>;
36
+ success: z.ZodLiteral<true>;
37
+ }>[k_1]; } : never>, z.ZodObject<{
38
+ data: z.ZodNever;
39
+ message: z.ZodString;
40
+ status: z.ZodOptional<z.ZodNumber>;
41
+ error: z.ZodObject<{
42
+ message: z.ZodString;
43
+ info: z.ZodObject<{
44
+ status: z.ZodNumber;
45
+ code: z.ZodString;
46
+ messages: z.ZodArray<z.ZodObject<{
47
+ message: z.ZodString;
48
+ rule: z.ZodOptional<z.ZodString>;
49
+ field: z.ZodOptional<z.ZodString>;
50
+ }, "strip", z.ZodTypeAny, {
51
+ message: string;
52
+ rule?: string | undefined;
53
+ field?: string | undefined;
54
+ }, {
55
+ message: string;
56
+ rule?: string | undefined;
57
+ field?: string | undefined;
58
+ }>, "many">;
59
+ }, "strip", z.ZodTypeAny, {
60
+ status: number;
61
+ code: string;
62
+ messages: {
63
+ message: string;
64
+ rule?: string | undefined;
65
+ field?: string | undefined;
66
+ }[];
67
+ }, {
68
+ status: number;
69
+ code: string;
70
+ messages: {
71
+ message: string;
72
+ rule?: string | undefined;
73
+ field?: string | undefined;
74
+ }[];
75
+ }>;
76
+ }, "strip", z.ZodTypeAny, {
77
+ message: string;
78
+ info: {
79
+ status: number;
80
+ code: string;
81
+ messages: {
82
+ message: string;
83
+ rule?: string | undefined;
84
+ field?: string | undefined;
85
+ }[];
86
+ };
87
+ }, {
88
+ message: string;
89
+ info: {
90
+ status: number;
91
+ code: string;
92
+ messages: {
93
+ message: string;
94
+ rule?: string | undefined;
95
+ field?: string | undefined;
96
+ }[];
97
+ };
98
+ }>;
99
+ success: z.ZodNever;
100
+ }, "strip", z.ZodTypeAny, {
101
+ message: string;
102
+ data: never;
103
+ error: {
104
+ message: string;
105
+ info: {
106
+ status: number;
107
+ code: string;
108
+ messages: {
109
+ message: string;
110
+ rule?: string | undefined;
111
+ field?: string | undefined;
112
+ }[];
113
+ };
114
+ };
115
+ success: never;
116
+ status?: number | undefined;
117
+ }, {
118
+ message: string;
119
+ data: never;
120
+ error: {
121
+ message: string;
122
+ info: {
123
+ status: number;
124
+ code: string;
125
+ messages: {
126
+ message: string;
127
+ rule?: string | undefined;
128
+ field?: string | undefined;
129
+ }[];
130
+ };
131
+ };
132
+ success: never;
133
+ status?: number | undefined;
134
+ }>]>;
135
+ export type IBaseRoute = {
136
+ url: string;
137
+ searchParams?: z.ZodObject<any>;
138
+ params?: z.ZodObject<any>;
139
+ response: ReturnType<typeof response>;
140
+ };
141
+ export type IRoute = IBaseRoute & ({
142
+ method: IHttpMethodWithBody;
143
+ body?: z.ZodObject<any>;
144
+ } | {
145
+ method: IHttpMethodWithoutBody;
146
+ body?: never;
147
+ });
148
+ export type NestedRoute = {
149
+ [K: string]: NestedRoute | IRoute;
150
+ };
151
+ export declare const IApiType: <T extends NestedRoute>(api: T) => T;
152
+ export type TransformApi<T extends NestedRoute> = {
153
+ [K in keyof T]: T[K] extends NestedRoute ? TransformApi<T[K]> : T[K]["body"] extends z.ZodObject<any> ? T[K]["searchParams"] extends z.ZodObject<any> ? T[K]["params"] extends z.ZodObject<any> ? (params: {
154
+ body: z.infer<T[K]["body"]>;
155
+ searchParams: z.infer<T[K]["searchParams"]>;
156
+ queryParams: z.infer<T[K]["params"]>;
157
+ }) => Promise<z.infer<T[K]["response"]>> : (params: {
158
+ body: z.infer<T[K]["body"]>;
159
+ searchParams: z.infer<T[K]["searchParams"]>;
160
+ }) => Promise<z.infer<T[K]["response"]>> : T[K]["params"] extends z.ZodObject<any> ? (params: {
161
+ body: z.infer<T[K]["body"]>;
162
+ queryParams: z.infer<T[K]["params"]>;
163
+ }) => Promise<z.infer<T[K]["response"]>> : (params: {
164
+ body: z.infer<T[K]["body"]>;
165
+ }) => Promise<z.infer<T[K]["response"]>> : T[K]["searchParams"] extends z.ZodObject<any> ? T[K]["params"] extends z.ZodObject<any> ? (params: {
166
+ searchParams: z.infer<T[K]["searchParams"]>;
167
+ queryParams: z.infer<T[K]["params"]>;
168
+ }) => Promise<z.infer<T[K]["response"]>> : (params: {
169
+ searchParams: z.infer<T[K]["searchParams"]>;
170
+ }) => Promise<z.infer<T[K]["response"]>> : T[K]["params"] extends z.ZodObject<any> ? (params: {
171
+ queryParams: z.infer<T[K]["params"]>;
172
+ }) => Promise<z.infer<T[K]["response"]>> : () => Promise<z.infer<T[K]["response"]>>;
173
+ };
174
+ export type ApiRouteResponse<Route extends (...args: any) => Promise<any>> = Awaited<ReturnType<Route>>["data"];
175
+ export type IRouterApi = TransformApi<ApiService>;
176
+ export type IRouteFunction = (...params: any[]) => Promise<IRoute["response"]>;
177
+ export type IRouteGroup = {
178
+ [name: string]: IRouteFunction | IRouteGroup;
179
+ };
180
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IApiType = exports.response = void 0;
4
+ const zod_1 = require("zod");
5
+ const response = (data) => {
6
+ return zod_1.z
7
+ .object({
8
+ data,
9
+ message: zod_1.z.string().optional(),
10
+ status: zod_1.z.number().optional(),
11
+ error: zod_1.z.never().optional(),
12
+ success: zod_1.z.literal(true),
13
+ })
14
+ .or(zod_1.z.object({
15
+ data: zod_1.z.never(),
16
+ message: zod_1.z.string(),
17
+ status: zod_1.z.number().optional(),
18
+ error: zod_1.z.object({
19
+ message: zod_1.z.string(),
20
+ info: zod_1.z.object({
21
+ status: zod_1.z.number(),
22
+ code: zod_1.z.string(),
23
+ messages: zod_1.z.array(zod_1.z.object({
24
+ message: zod_1.z.string(),
25
+ rule: zod_1.z.string().optional(),
26
+ field: zod_1.z.string().optional(),
27
+ })),
28
+ }),
29
+ }),
30
+ success: zod_1.z.never(),
31
+ }));
32
+ };
33
+ exports.response = response;
34
+ const IApiType = (api) => api;
35
+ exports.IApiType = IApiType;
@@ -0,0 +1,16 @@
1
+ import { AxiosHeaders } from "axios";
2
+ import { IRoute, IRouteGroup, IRouterApi } from "./_type";
3
+ import { NestedRoute } from "./_type";
4
+ export declare const apiUrlBuilder: (segment?: string, baseUrl?: string, host?: string) => string;
5
+ export declare const apiFetch: {
6
+ setHostApi: (h: string) => void;
7
+ setBaseUrl: (b: string) => void;
8
+ setHeaders: (h: AxiosHeaders | undefined) => void;
9
+ fetch: (urlInput: string, method?: RequestInit["method"] | undefined, body?: any, searchParams?: any, queryParams?: {
10
+ [key: string]: string;
11
+ }) => Promise<any>;
12
+ };
13
+ export declare const isRoute: (value: NestedRoute | IRoute) => value is IRoute;
14
+ export declare const isSubRoute: (value: NestedRoute | IRoute) => value is NestedRoute;
15
+ export declare const parseApiRoute: <T extends NestedRoute | IRoute, K extends IRouteGroup>(key: string | undefined, subRoute: T, result: K, hostApi: string, basePath: string, headers?: AxiosHeaders) => K;
16
+ export declare const parseApiRoutes: <T extends NestedRoute>(api: T, hostApi: string, basePath: string, headers?: AxiosHeaders) => IRouterApi;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parseApiRoutes = exports.parseApiRoute = exports.isSubRoute = exports.isRoute = exports.apiFetch = exports.apiUrlBuilder = void 0;
7
+ const path_1 = require("path");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ let host;
10
+ let baseUrl;
11
+ let headers;
12
+ const apiUrlBuilder = (segment = "", baseUrl = "", host = "") => {
13
+ const path = (0, path_1.join)(baseUrl, segment);
14
+ return host + path;
15
+ };
16
+ exports.apiUrlBuilder = apiUrlBuilder;
17
+ exports.apiFetch = {
18
+ setHostApi: (h) => {
19
+ host = h;
20
+ },
21
+ setBaseUrl: (b) => {
22
+ baseUrl = b;
23
+ },
24
+ setHeaders: (h) => {
25
+ headers = h;
26
+ },
27
+ fetch: async (urlInput, method, body, searchParams, queryParams) => {
28
+ try {
29
+ let url = (0, exports.apiUrlBuilder)(urlInput, baseUrl, host);
30
+ if (queryParams) {
31
+ Object.entries(queryParams).forEach(([key, value]) => {
32
+ if (value !== undefined && value !== null) {
33
+ url = url.replace(`:${key}`, encodeURIComponent(String(value)));
34
+ }
35
+ });
36
+ }
37
+ if (searchParams) {
38
+ const searchParamsString = typeof searchParams === "string"
39
+ ? searchParams
40
+ : Object.entries(searchParams)
41
+ .filter(([_, value]) => value !== undefined && value !== null)
42
+ .map(([key, value]) => {
43
+ const encodedKey = encodeURIComponent(key);
44
+ const encodedValue = Array.isArray(value) || typeof value === "object"
45
+ ? encodeURIComponent(JSON.stringify(value))
46
+ : encodeURIComponent(String(value !== null && value !== void 0 ? value : ""));
47
+ return `${encodedKey}=${encodedValue}`;
48
+ })
49
+ .join("&");
50
+ url += (url.includes("?") ? "&" : "?") + searchParamsString;
51
+ }
52
+ const res = await (0, axios_1.default)({
53
+ headers: {
54
+ "Content-Type": "application/json",
55
+ "Access-Control-Allow-Origin": "*",
56
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
57
+ "Access-Control-Allow-Headers": "Content-Type, Authorization",
58
+ "Access-Control-Allow-Credentials": "true",
59
+ "Access-Control-Allow-Private-Network": "true",
60
+ Accept: "application/json",
61
+ ...headers,
62
+ },
63
+ method,
64
+ url,
65
+ data: body,
66
+ });
67
+ return res.data;
68
+ }
69
+ catch (error) {
70
+ throw new Error(`Request failed: ${error}`);
71
+ }
72
+ },
73
+ };
74
+ const isRoute = (value) => {
75
+ return "method" in value;
76
+ };
77
+ exports.isRoute = isRoute;
78
+ const isSubRoute = (value) => {
79
+ return !(0, exports.isRoute)(value);
80
+ };
81
+ exports.isSubRoute = isSubRoute;
82
+ const createRouteFunction = (route, apiFetch) => {
83
+ const createFetchFunction = (paramTypes) => {
84
+ return async (...args) => {
85
+ const params = paramTypes.reduce((acc, type) => {
86
+ acc[type] = args[0][type] || null;
87
+ return acc;
88
+ }, {});
89
+ return await apiFetch.fetch(route.url, route.method, params.body, params.searchParams, params.queryParams);
90
+ };
91
+ };
92
+ if (route.body) {
93
+ if (route.searchParams) {
94
+ return route.params
95
+ ? createFetchFunction(["body", "searchParams", "queryParams"])
96
+ : createFetchFunction(["body", "searchParams"]);
97
+ }
98
+ return route.params ? createFetchFunction(["body", "queryParams"]) : createFetchFunction(["body"]);
99
+ }
100
+ if (route.searchParams) {
101
+ return route.params ? createFetchFunction(["searchParams", "queryParams"]) : createFetchFunction(["searchParams"]);
102
+ }
103
+ return route.params ? createFetchFunction(["queryParams"]) : createFetchFunction([]);
104
+ };
105
+ const parseApiRoute = (key, subRoute, result, hostApi, basePath, headers) => {
106
+ var _a;
107
+ exports.apiFetch.setHostApi(hostApi);
108
+ exports.apiFetch.setBaseUrl(basePath);
109
+ exports.apiFetch.setHeaders(headers);
110
+ if ((0, exports.isRoute)(subRoute) && key) {
111
+ ;
112
+ result = createRouteFunction(subRoute, exports.apiFetch);
113
+ }
114
+ else if ((0, exports.isSubRoute)(subRoute)) {
115
+ for (const [key, value] of Object.entries(subRoute)) {
116
+ ;
117
+ result[key] = (0, exports.parseApiRoute)(key, value, ((_a = result[key]) !== null && _a !== void 0 ? _a : {}), hostApi, basePath, headers);
118
+ }
119
+ }
120
+ return result;
121
+ };
122
+ exports.parseApiRoute = parseApiRoute;
123
+ const parseApiRoutes = (api, hostApi, basePath, headers) => {
124
+ const result = {};
125
+ const response = (0, exports.parseApiRoute)(undefined, api, result, hostApi, basePath, headers);
126
+ return response;
127
+ };
128
+ exports.parseApiRoutes = parseApiRoutes;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.index = void 0;
4
4
  const public_1 = require("./../routes/public");
5
5
  const user_1 = require("./../routes/user");
6
- const _type_1 = require("./_type");
6
+ const _type_1 = require("./api/_type");
7
7
  const driver_1 = require("./../routes/driver");
8
8
  const admin_1 = require("./../routes/admin");
9
9
  const ops_1 = require("./../routes/ops");
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { index } from "./core/index";
2
- import { TransformApi } from "./core/_type";
2
+ import { TransformApi } from "./core/api/_type";
3
3
  import { AxiosHeaderValue } from "axios";
4
4
  type RecordHeaders = {
5
5
  [x: string]: AxiosHeaderValue | undefined;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.APIRouter = void 0;
4
- const _untils_1 = require("./core/_untils");
4
+ const _untils_1 = require("./core/api/_untils");
5
5
  const index_1 = require("./core/index");
6
6
  const axios_1 = require("axios");
7
7
  class APIRouter {
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.adminRoutes = void 0;
4
4
  const zod_1 = require("zod");
5
- const _type_1 = require("../core/_type");
5
+ const _type_1 = require("../core/api/_type");
6
6
  const models_1 = require("./../models");
7
7
  const _user_1 = require("./../models/_user");
8
8
  const baseurl = "/admin";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.driverRoutes = void 0;
4
- const _type_1 = require("../core/_type");
4
+ const _type_1 = require("../core/api/_type");
5
5
  // const baseurl = "/user"
6
6
  exports.driverRoutes = (0, _type_1.IApiType)({});
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.opsRoutes = void 0;
4
4
  const zod_1 = require("zod");
5
- const _type_1 = require("../core/_type");
5
+ const _type_1 = require("../core/api/_type");
6
6
  const models_1 = require("./../models");
7
7
  const baseurl = "/ops";
8
8
  const dashboardRoutes = (0, _type_1.IApiType)({
@@ -642,6 +642,16 @@ export declare const publicRoutes: {
642
642
  bipTelepage: string;
643
643
  } | undefined;
644
644
  }>;
645
+ ws: z.ZodObject<{
646
+ user: z.ZodString;
647
+ roles: z.ZodArray<z.ZodString, "many">;
648
+ }, "strip", z.ZodTypeAny, {
649
+ roles: string[];
650
+ user: string;
651
+ }, {
652
+ roles: string[];
653
+ user: string;
654
+ }>;
645
655
  }, "strip", z.ZodTypeAny, {
646
656
  token: {
647
657
  type: "Bearer";
@@ -801,6 +811,10 @@ export declare const publicRoutes: {
801
811
  lastUsedAt?: string | undefined;
802
812
  expiresAt?: string | undefined;
803
813
  };
814
+ ws: {
815
+ roles: string[];
816
+ user: string;
817
+ };
804
818
  }, {
805
819
  token: {
806
820
  type: "Bearer";
@@ -960,6 +974,10 @@ export declare const publicRoutes: {
960
974
  lastUsedAt?: string | undefined;
961
975
  expiresAt?: string | undefined;
962
976
  };
977
+ ws: {
978
+ roles: string[];
979
+ user: string;
980
+ };
963
981
  }>;
964
982
  message: z.ZodOptional<z.ZodString>;
965
983
  status: z.ZodOptional<z.ZodNumber>;
@@ -1125,6 +1143,10 @@ export declare const publicRoutes: {
1125
1143
  lastUsedAt?: string | undefined;
1126
1144
  expiresAt?: string | undefined;
1127
1145
  };
1146
+ ws: {
1147
+ roles: string[];
1148
+ user: string;
1149
+ };
1128
1150
  };
1129
1151
  success: true;
1130
1152
  status?: number | undefined;
@@ -1290,6 +1312,10 @@ export declare const publicRoutes: {
1290
1312
  lastUsedAt?: string | undefined;
1291
1313
  expiresAt?: string | undefined;
1292
1314
  };
1315
+ ws: {
1316
+ roles: string[];
1317
+ user: string;
1318
+ };
1293
1319
  };
1294
1320
  success: true;
1295
1321
  status?: number | undefined;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.publicRoutes = void 0;
4
4
  const zod_1 = require("zod");
5
- const _type_1 = require("../core/_type");
5
+ const _type_1 = require("../core/api/_type");
6
6
  const models_1 = require("./../models");
7
7
  const baseurl = "";
8
8
  exports.publicRoutes = (0, _type_1.IApiType)({
@@ -15,6 +15,10 @@ exports.publicRoutes = (0, _type_1.IApiType)({
15
15
  token: models_1.TokenSchema,
16
16
  refreshToken: models_1.TokenSchema,
17
17
  user: models_1.UserSchema,
18
+ ws: zod_1.z.object({
19
+ user: zod_1.z.string(),
20
+ roles: zod_1.z.array(zod_1.z.string()),
21
+ }),
18
22
  })),
19
23
  },
20
24
  forgotPassword: {
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.userRoutes = void 0;
4
4
  const zod_1 = require("zod");
5
- const _type_1 = require("../core/_type");
5
+ const _type_1 = require("../core/api/_type");
6
6
  const models_1 = require("./../models");
7
7
  const _untils_1 = require("./../models/_untils");
8
8
  const baseurl = "/user";
@@ -111,7 +111,7 @@ const shippingRoutes = (0, _type_1.IApiType)({
111
111
  },
112
112
  create: {
113
113
  method: "POST",
114
- url: `${baseurl}/shipping/:userInterface/create`,
114
+ url: `${baseurl}/shipping/:userInterface/create/:typeExpedition`,
115
115
  params: zod_1.z.object({
116
116
  userInterface: zod_1.z.enum(["paris", "medical", "service"]),
117
117
  typeExpedition: zod_1.z.enum(["import", "export", "course"]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exnet-routing",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [