@intrig/plugin-react 0.0.1
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.
- package/.swcrc +29 -0
- package/README.md +7 -0
- package/eslint.config.mjs +19 -0
- package/package.json +25 -0
- package/project.json +29 -0
- package/rollup.config.cjs +54 -0
- package/rollup.config.mjs +33 -0
- package/src/index.ts +2 -0
- package/src/lib/code-generator.ts +79 -0
- package/src/lib/get-endpoint-documentation.ts +35 -0
- package/src/lib/get-schema-documentation.ts +11 -0
- package/src/lib/internal-types.ts +15 -0
- package/src/lib/plugin-react.ts +22 -0
- package/src/lib/templates/context.template.ts +74 -0
- package/src/lib/templates/docs/__snapshots__/async-hook.spec.ts.snap +889 -0
- package/src/lib/templates/docs/__snapshots__/download-hook.spec.ts.snap +1445 -0
- package/src/lib/templates/docs/__snapshots__/react-hook.spec.ts.snap +1371 -0
- package/src/lib/templates/docs/__snapshots__/sse-hook.spec.ts.snap +2008 -0
- package/src/lib/templates/docs/async-hook.spec.ts +92 -0
- package/src/lib/templates/docs/async-hook.ts +226 -0
- package/src/lib/templates/docs/download-hook.spec.ts +182 -0
- package/src/lib/templates/docs/download-hook.ts +170 -0
- package/src/lib/templates/docs/react-hook.spec.ts +97 -0
- package/src/lib/templates/docs/react-hook.ts +323 -0
- package/src/lib/templates/docs/schema.ts +105 -0
- package/src/lib/templates/docs/sse-hook.spec.ts +207 -0
- package/src/lib/templates/docs/sse-hook.ts +221 -0
- package/src/lib/templates/extra.template.ts +198 -0
- package/src/lib/templates/index.template.ts +14 -0
- package/src/lib/templates/intrigMiddleware.template.ts +21 -0
- package/src/lib/templates/logger.template.ts +67 -0
- package/src/lib/templates/media-type-utils.template.ts +191 -0
- package/src/lib/templates/network-state.template.ts +702 -0
- package/src/lib/templates/packageJson.template.ts +63 -0
- package/src/lib/templates/provider/__tests__/provider-templates.spec.ts +209 -0
- package/src/lib/templates/provider/axios-config.template.ts +49 -0
- package/src/lib/templates/provider/hooks.template.ts +240 -0
- package/src/lib/templates/provider/interfaces.template.ts +72 -0
- package/src/lib/templates/provider/intrig-provider-stub.template.ts +73 -0
- package/src/lib/templates/provider/intrig-provider.template.ts +185 -0
- package/src/lib/templates/provider/main.template.ts +48 -0
- package/src/lib/templates/provider/reducer.template.ts +50 -0
- package/src/lib/templates/provider/status-trap.template.ts +80 -0
- package/src/lib/templates/provider.template.ts +698 -0
- package/src/lib/templates/source/controller/method/asyncFunctionHook.template.ts +196 -0
- package/src/lib/templates/source/controller/method/clientIndex.template.ts +38 -0
- package/src/lib/templates/source/controller/method/download.template.ts +256 -0
- package/src/lib/templates/source/controller/method/params.template.ts +31 -0
- package/src/lib/templates/source/controller/method/requestHook.template.ts +220 -0
- package/src/lib/templates/source/type/typeTemplate.ts +257 -0
- package/src/lib/templates/swcrc.template.ts +25 -0
- package/src/lib/templates/tsconfig.template.ts +37 -0
- package/src/lib/templates/type-utils.template.ts +28 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +20 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {typescript} from "@intrig/plugin-sdk";
|
|
2
|
+
import * as path from 'path'
|
|
3
|
+
|
|
4
|
+
export function reactLoggerTemplate() {
|
|
5
|
+
const ts = typescript(path.resolve('src', 'logger.ts'))
|
|
6
|
+
|
|
7
|
+
return ts`
|
|
8
|
+
// logger.ts
|
|
9
|
+
|
|
10
|
+
import log, { LogLevelDesc } from 'loglevel';
|
|
11
|
+
|
|
12
|
+
// Extend the global interfaces
|
|
13
|
+
declare global {
|
|
14
|
+
interface Window {
|
|
15
|
+
setLogLevel?: (level: LogLevelDesc) => void;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 1) Build-time default via Vite (if available)
|
|
20
|
+
// Cast import.meta to any to avoid TS errors if env isn't typed
|
|
21
|
+
const buildDefault =
|
|
22
|
+
typeof import.meta !== 'undefined'
|
|
23
|
+
? ((import.meta as any).env?.VITE_LOG_LEVEL as string | undefined)
|
|
24
|
+
: undefined;
|
|
25
|
+
|
|
26
|
+
// 2) Stored default in localStorage
|
|
27
|
+
const storedLevel =
|
|
28
|
+
typeof localStorage !== 'undefined'
|
|
29
|
+
? (localStorage.getItem('LOG_LEVEL') as string | null)
|
|
30
|
+
: null;
|
|
31
|
+
|
|
32
|
+
// Determine initial log level: build-time → stored → 'error'
|
|
33
|
+
const defaultLevel: LogLevelDesc =
|
|
34
|
+
(buildDefault as LogLevelDesc) ?? (storedLevel as LogLevelDesc) ?? 'error';
|
|
35
|
+
|
|
36
|
+
// Apply initial level
|
|
37
|
+
log.setLevel(defaultLevel);
|
|
38
|
+
|
|
39
|
+
// Expose a console setter to change level at runtime
|
|
40
|
+
if (typeof window !== 'undefined') {
|
|
41
|
+
window.setLogLevel = (level: LogLevelDesc): void => {
|
|
42
|
+
log.setLevel(level);
|
|
43
|
+
try {
|
|
44
|
+
localStorage.setItem('LOG_LEVEL', String(level));
|
|
45
|
+
} catch {
|
|
46
|
+
// ignore if storage is unavailable
|
|
47
|
+
}
|
|
48
|
+
console.log(\`✏️ loglevel set to '\${level}'\`);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Consistent wrapper API
|
|
53
|
+
export const logger = {
|
|
54
|
+
info: (msg: unknown, meta?: unknown): void =>
|
|
55
|
+
meta ? log.info(msg, meta) : log.info(msg),
|
|
56
|
+
warn: (msg: unknown, meta?: unknown): void =>
|
|
57
|
+
meta ? log.warn(msg, meta) : log.warn(msg),
|
|
58
|
+
error: (msg: unknown, meta?: unknown): void =>
|
|
59
|
+
meta ? log.error(msg, meta) : log.error(msg),
|
|
60
|
+
debug: (msg: unknown, meta?: unknown): void =>
|
|
61
|
+
meta ? log.debug(msg, meta) : log.debug(msg),
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default logger;
|
|
65
|
+
|
|
66
|
+
`
|
|
67
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import {typescript} from "@intrig/plugin-sdk";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
|
|
4
|
+
export function reactMediaTypeUtilsTemplate() {
|
|
5
|
+
const ts = typescript(path.resolve("src", "media-type-utils.ts"))
|
|
6
|
+
|
|
7
|
+
return ts`
|
|
8
|
+
import { ZodSchema } from 'zod';
|
|
9
|
+
import { XMLParser } from 'fast-xml-parser';
|
|
10
|
+
type EncodersSync = {
|
|
11
|
+
[k: string]: <T>(request: T,
|
|
12
|
+
mediaType: string,
|
|
13
|
+
schema: ZodSchema) => any;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const encoders: EncodersSync = {};
|
|
17
|
+
|
|
18
|
+
export function encode<T>(request: T, mediaType: string, schema: ZodSchema) {
|
|
19
|
+
if (encoders[mediaType]) {
|
|
20
|
+
return encoders[mediaType](request, mediaType, schema);
|
|
21
|
+
}
|
|
22
|
+
return request;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
encoders['application/json'] = (request, mediaType, schema) => {
|
|
26
|
+
return request;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function appendFormData(
|
|
30
|
+
formData: FormData,
|
|
31
|
+
data: any,
|
|
32
|
+
parentKey: string
|
|
33
|
+
): void {
|
|
34
|
+
if (data instanceof Blob || typeof data === 'string') {
|
|
35
|
+
formData.append(parentKey, data);
|
|
36
|
+
} else if (data !== null && typeof data === 'object') {
|
|
37
|
+
if (Array.isArray(data)) {
|
|
38
|
+
data.forEach((item: any, index: number) => {
|
|
39
|
+
const key = ${"`${parentKey}`"};
|
|
40
|
+
appendFormData(formData, item, key);
|
|
41
|
+
});
|
|
42
|
+
} else {
|
|
43
|
+
Object.keys(data).forEach((key: string) => {
|
|
44
|
+
const newKey = parentKey ? ${"`${parentKey}[${key}]`"} : key;
|
|
45
|
+
appendFormData(formData, data[key], newKey);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
formData.append(parentKey, data == null ? '' : String(data));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
encoders['multipart/form-data'] = (request, mediaType, schema) => {
|
|
54
|
+
const _request = request as Record<string, any>;
|
|
55
|
+
const formData = new FormData();
|
|
56
|
+
Object.keys(_request).forEach((key: string) => {
|
|
57
|
+
appendFormData(formData, _request[key], key);
|
|
58
|
+
});
|
|
59
|
+
return formData;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
encoders['application/octet-stream'] = (request, mediaType, schema) => {
|
|
63
|
+
return request;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
encoders['application/x-www-form-urlencoded'] = (request, mediaType, schema) => {
|
|
67
|
+
const formData = new FormData();
|
|
68
|
+
for (const key in request) {
|
|
69
|
+
const value = request[key];
|
|
70
|
+
formData.append(key, value instanceof Blob || typeof value === 'string' ? value : String(value));
|
|
71
|
+
}
|
|
72
|
+
return formData;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type Transformers = {
|
|
76
|
+
[k: string]: <T>(
|
|
77
|
+
request: Request,
|
|
78
|
+
mediaType: string,
|
|
79
|
+
schema: ZodSchema
|
|
80
|
+
) => Promise<T>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const transformers: Transformers = {};
|
|
84
|
+
|
|
85
|
+
export function transform<T>(
|
|
86
|
+
request: Request,
|
|
87
|
+
mediaType: string,
|
|
88
|
+
schema: ZodSchema
|
|
89
|
+
): Promise<T> {
|
|
90
|
+
if (transformers[mediaType]) {
|
|
91
|
+
return transformers[mediaType](request, mediaType, schema);
|
|
92
|
+
}
|
|
93
|
+
throw new Error(\`Unsupported media type: \` + mediaType);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
transformers['application/json'] = async (request, mediaType, schema) => {
|
|
97
|
+
return schema.parse(await request.json());
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
transformers['multipart/form-data'] = async (request, mediaType, schema) => {
|
|
101
|
+
const formData = await request.formData();
|
|
102
|
+
const content: Record<string, any> = {};
|
|
103
|
+
formData.forEach((value, key) => {
|
|
104
|
+
if (content[key]) {
|
|
105
|
+
if (!(content[key] instanceof Array)) {
|
|
106
|
+
content[key] = [content[key]];
|
|
107
|
+
}
|
|
108
|
+
content[key].push(value);
|
|
109
|
+
} else {
|
|
110
|
+
content[key] = value
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return schema.parse(content);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
transformers['application/octet-stream'] = async (
|
|
117
|
+
request,
|
|
118
|
+
mediaType,
|
|
119
|
+
schema
|
|
120
|
+
) => {
|
|
121
|
+
return schema.parse(
|
|
122
|
+
new Blob([await request.arrayBuffer()], {
|
|
123
|
+
type: 'application/octet-stream',
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
transformers['application/x-www-form-urlencoded'] = async (
|
|
129
|
+
request,
|
|
130
|
+
mediaType,
|
|
131
|
+
schema
|
|
132
|
+
) => {
|
|
133
|
+
const formData = await request.formData();
|
|
134
|
+
const content: Record<string, any> = {};
|
|
135
|
+
formData.forEach((value, key) => (content[key] = value));
|
|
136
|
+
return schema.parse(content);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
transformers['application/xml'] = async (request, mediaType, schema) => {
|
|
140
|
+
const xmlParser = new XMLParser();
|
|
141
|
+
const content = await xmlParser.parse(await request.text());
|
|
142
|
+
return schema.parse(await content);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
transformers['text/plain'] = async (request, mediaType, schema) => {
|
|
146
|
+
return schema.parse(await request.text());
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
transformers['text/html'] = async (request, mediaType, schema) => {
|
|
150
|
+
return schema.parse(await request.text());
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
transformers['text/css'] = async (request, mediaType, schema) => {
|
|
154
|
+
return schema.parse(await request.text());
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
transformers['text/javascript'] = async (request, mediaType, schema) => {
|
|
158
|
+
return schema.parse(await request.text());
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
type ResponseTransformers = {
|
|
162
|
+
[k: string]: <T>(
|
|
163
|
+
data: any,
|
|
164
|
+
mediaType: string,
|
|
165
|
+
schema: ZodSchema
|
|
166
|
+
) => Promise<T>;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const responseTransformers: ResponseTransformers = {};
|
|
170
|
+
|
|
171
|
+
responseTransformers['application/json'] = async (data, mediaType, schema) => {
|
|
172
|
+
return schema.parse(data);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
responseTransformers['application/xml'] = async (data, mediaType, schema) => {
|
|
176
|
+
const parsed = new XMLParser().parse(data);
|
|
177
|
+
return schema.parse(parsed);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export async function transformResponse<T>(
|
|
181
|
+
data: any,
|
|
182
|
+
mediaType: string,
|
|
183
|
+
schema: ZodSchema
|
|
184
|
+
): Promise<T> {
|
|
185
|
+
if (responseTransformers[mediaType]) {
|
|
186
|
+
return await responseTransformers[mediaType](data, mediaType, schema);
|
|
187
|
+
}
|
|
188
|
+
return data
|
|
189
|
+
}
|
|
190
|
+
`
|
|
191
|
+
}
|