@mgamadeus/ddd-web 0.6.1 → 0.7.0

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,43 @@
1
+ /**
2
+ * Vite plugin: binds the `Request` that RTK Query's `fetchBaseQuery` constructs to `FetchApiRequest`
3
+ * (`@mgamadeus/ddd-web/fetch-api-request`), for apps that run inside pages whose global `Request` is not the Fetch API's
4
+ * (MooTools 1.x, Prototype). The binding is an import prepended to the toolkit's query module; it shadows the global
5
+ * there and nowhere else, so the page's own scripts keep their class.
6
+ *
7
+ * Build only (a watch build included). The dev server pre-bundles the toolkit untransformed (`optimizeDeps`), so a page
8
+ * with a foreign `Request` served by the dev server is not covered; the base query then refuses the request with a
9
+ * message that names the cause. The build fails when the toolkit's query module is part of it and is not bound (its
10
+ * file moved, or it no longer constructs a `Request`), so a toolkit update cannot bring the defect back unnoticed.
11
+ *
12
+ * @example
13
+ * // vite.config.ts
14
+ * import { dddFetchApiRequestPlugin } from '@mgamadeus/ddd-web/vite';
15
+ * export default defineConfig({ plugins: [react(), dddFetchApiRequestPlugin()] });
16
+ */
17
+ /** The parts of a Rollup plugin context the plugin uses. */
18
+ interface DddFetchApiRequestPluginContext {
19
+ error(message: string): never;
20
+ getModuleIds(): IterableIterator<string>;
21
+ getModuleInfo(moduleId: string): {
22
+ code: string | null;
23
+ } | null;
24
+ }
25
+ /** The plugin object (a Vite `Plugin`; typed structurally, so the package needs no Vite dependency). */
26
+ interface DddFetchApiRequestPlugin {
27
+ name: string;
28
+ apply: 'build';
29
+ enforce: 'pre';
30
+ transform(this: DddFetchApiRequestPluginContext, code: string, moduleId: string): {
31
+ code: string;
32
+ map: null;
33
+ } | null;
34
+ buildEnd(this: DddFetchApiRequestPluginContext, buildError?: Error): void;
35
+ }
36
+ interface DddFetchApiRequestPluginOptions {
37
+ /** Module that exports `FetchApiRequest`. Default `@mgamadeus/ddd-web/fetch-api-request`. */
38
+ fetchApiRequestModule?: string;
39
+ }
40
+ declare function dddFetchApiRequestPlugin(options?: DddFetchApiRequestPluginOptions): DddFetchApiRequestPlugin;
41
+
42
+ export { dddFetchApiRequestPlugin };
43
+ export type { DddFetchApiRequestPlugin, DddFetchApiRequestPluginContext, DddFetchApiRequestPluginOptions };
@@ -0,0 +1,39 @@
1
+ // src/vite/index.ts
2
+ var TOOLKIT_QUERY_MODULE_PATH = /[\\/]@reduxjs[\\/]toolkit[\\/]dist[\\/]query[\\/]rtk-query\.[\w.-]+\.m?js$/;
3
+ var TOOLKIT_QUERY_FOLDER_PATH = /[\\/]@reduxjs[\\/]toolkit[\\/]dist[\\/]query[\\/][^\\/]+\.m?js$/;
4
+ var REQUEST_CONSTRUCTION = "new Request(";
5
+ var REQUEST_BINDING_NAME = "FetchApiRequest";
6
+ var readModulePath = (moduleId) => moduleId.split("?")[0];
7
+ function dddFetchApiRequestPlugin(options = {}) {
8
+ const fetchApiRequestModule = options.fetchApiRequestModule ?? "@mgamadeus/ddd-web/fetch-api-request";
9
+ const requestBinding = `import { ${REQUEST_BINDING_NAME} as Request } from ${JSON.stringify(fetchApiRequestModule)};`;
10
+ return {
11
+ name: "ddd-web:fetch-api-request-in-toolkit-query",
12
+ apply: "build",
13
+ enforce: "pre",
14
+ transform(code, moduleId) {
15
+ if (!TOOLKIT_QUERY_MODULE_PATH.test(readModulePath(moduleId))) return null;
16
+ if (!code.includes(REQUEST_CONSTRUCTION)) {
17
+ this.error(`${moduleId} constructs no Request any more: check how the toolkit builds its requests now.`);
18
+ }
19
+ return { code: `${requestBinding}${code}`, map: null };
20
+ },
21
+ /** Reads the built modules and keeps no state: a watch rebuild takes the bound toolkit module from its cache. */
22
+ buildEnd(buildError) {
23
+ if (buildError) return;
24
+ const toolkitQueryModuleIds = [...this.getModuleIds()].filter((moduleId) => TOOLKIT_QUERY_FOLDER_PATH.test(readModulePath(moduleId)));
25
+ const unboundModuleIds = toolkitQueryModuleIds.filter((moduleId) => {
26
+ const moduleCode = this.getModuleInfo(moduleId)?.code ?? "";
27
+ return moduleCode.includes(REQUEST_CONSTRUCTION) && !moduleCode.includes(REQUEST_BINDING_NAME);
28
+ });
29
+ const isQueryModulePartOfBuild = toolkitQueryModuleIds.some((moduleId) => TOOLKIT_QUERY_MODULE_PATH.test(readModulePath(moduleId)));
30
+ if (unboundModuleIds.length > 0 || toolkitQueryModuleIds.length > 0 && !isQueryModulePartOfBuild) {
31
+ this.error(`ddd-web: the toolkit's query module is part of this build but its Request is not bound: ${toolkitQueryModuleIds.join(", ")}`);
32
+ }
33
+ }
34
+ };
35
+ }
36
+ export {
37
+ dddFetchApiRequestPlugin
38
+ };
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/vite/index.ts"],"sourcesContent":["/**\n * Vite plugin: binds the `Request` that RTK Query's `fetchBaseQuery` constructs to `FetchApiRequest`\n * (`@mgamadeus/ddd-web/fetch-api-request`), for apps that run inside pages whose global `Request` is not the Fetch API's\n * (MooTools 1.x, Prototype). The binding is an import prepended to the toolkit's query module; it shadows the global\n * there and nowhere else, so the page's own scripts keep their class.\n *\n * Build only (a watch build included). The dev server pre-bundles the toolkit untransformed (`optimizeDeps`), so a page\n * with a foreign `Request` served by the dev server is not covered; the base query then refuses the request with a\n * message that names the cause. The build fails when the toolkit's query module is part of it and is not bound (its\n * file moved, or it no longer constructs a `Request`), so a toolkit update cannot bring the defect back unnoticed.\n *\n * @example\n * // vite.config.ts\n * import { dddFetchApiRequestPlugin } from '@mgamadeus/ddd-web/vite';\n * export default defineConfig({ plugins: [react(), dddFetchApiRequestPlugin()] });\n */\n\n/** The parts of a Rollup plugin context the plugin uses. */\nexport interface DddFetchApiRequestPluginContext {\n error(message: string): never;\n getModuleIds(): IterableIterator<string>;\n getModuleInfo(moduleId: string): { code: string | null } | null;\n}\n\n/** The plugin object (a Vite `Plugin`; typed structurally, so the package needs no Vite dependency). */\nexport interface DddFetchApiRequestPlugin {\n name: string;\n apply: 'build';\n enforce: 'pre';\n transform(this: DddFetchApiRequestPluginContext, code: string, moduleId: string): { code: string; map: null } | null;\n buildEnd(this: DddFetchApiRequestPluginContext, buildError?: Error): void;\n}\n\nexport interface DddFetchApiRequestPluginOptions {\n /** Module that exports `FetchApiRequest`. Default `@mgamadeus/ddd-web/fetch-api-request`. */\n fetchApiRequestModule?: string;\n}\n\nconst TOOLKIT_QUERY_MODULE_PATH = /[\\\\/]@reduxjs[\\\\/]toolkit[\\\\/]dist[\\\\/]query[\\\\/]rtk-query\\.[\\w.-]+\\.m?js$/;\nconst TOOLKIT_QUERY_FOLDER_PATH = /[\\\\/]@reduxjs[\\\\/]toolkit[\\\\/]dist[\\\\/]query[\\\\/][^\\\\/]+\\.m?js$/;\nconst REQUEST_CONSTRUCTION = 'new Request(';\nconst REQUEST_BINDING_NAME = 'FetchApiRequest';\n\nconst readModulePath = (moduleId: string): string => moduleId.split('?')[0] as string;\n\nexport function dddFetchApiRequestPlugin(options: DddFetchApiRequestPluginOptions = {}): DddFetchApiRequestPlugin {\n const fetchApiRequestModule = options.fetchApiRequestModule ?? '@mgamadeus/ddd-web/fetch-api-request';\n // On the module's first line, without a line break: every line keeps its number in the source map.\n const requestBinding = `import { ${REQUEST_BINDING_NAME} as Request } from ${JSON.stringify(fetchApiRequestModule)};`;\n return {\n name: 'ddd-web:fetch-api-request-in-toolkit-query',\n apply: 'build',\n enforce: 'pre',\n transform(code, moduleId) {\n if (!TOOLKIT_QUERY_MODULE_PATH.test(readModulePath(moduleId))) return null;\n if (!code.includes(REQUEST_CONSTRUCTION)) {\n this.error(`${moduleId} constructs no Request any more: check how the toolkit builds its requests now.`);\n }\n return { code: `${requestBinding}${code}`, map: null };\n },\n /** Reads the built modules and keeps no state: a watch rebuild takes the bound toolkit module from its cache. */\n buildEnd(buildError) {\n if (buildError) return;\n const toolkitQueryModuleIds = [...this.getModuleIds()].filter((moduleId) => TOOLKIT_QUERY_FOLDER_PATH.test(readModulePath(moduleId)));\n const unboundModuleIds = toolkitQueryModuleIds.filter((moduleId) => {\n const moduleCode = this.getModuleInfo(moduleId)?.code ?? '';\n return moduleCode.includes(REQUEST_CONSTRUCTION) && !moduleCode.includes(REQUEST_BINDING_NAME);\n });\n const isQueryModulePartOfBuild = toolkitQueryModuleIds.some((moduleId) => TOOLKIT_QUERY_MODULE_PATH.test(readModulePath(moduleId)));\n if (unboundModuleIds.length > 0 || (toolkitQueryModuleIds.length > 0 && !isQueryModulePartOfBuild)) {\n this.error(`ddd-web: the toolkit's query module is part of this build but its Request is not bound: ${toolkitQueryModuleIds.join(', ')}`);\n }\n },\n };\n}\n"],"mappings":";AAsCA,IAAM,4BAA4B;AAClC,IAAM,4BAA4B;AAClC,IAAM,uBAAuB;AAC7B,IAAM,uBAAuB;AAE7B,IAAM,iBAAiB,CAAC,aAA6B,SAAS,MAAM,GAAG,EAAE,CAAC;AAEnE,SAAS,yBAAyB,UAA2C,CAAC,GAA6B;AAChH,QAAM,wBAAwB,QAAQ,yBAAyB;AAE/D,QAAM,iBAAiB,YAAY,oBAAoB,sBAAsB,KAAK,UAAU,qBAAqB,CAAC;AAClH,SAAO;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,IACT,UAAU,MAAM,UAAU;AACxB,UAAI,CAAC,0BAA0B,KAAK,eAAe,QAAQ,CAAC,EAAG,QAAO;AACtE,UAAI,CAAC,KAAK,SAAS,oBAAoB,GAAG;AACxC,aAAK,MAAM,GAAG,QAAQ,iFAAiF;AAAA,MACzG;AACA,aAAO,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI,IAAI,KAAK,KAAK;AAAA,IACvD;AAAA;AAAA,IAEA,SAAS,YAAY;AACnB,UAAI,WAAY;AAChB,YAAM,wBAAwB,CAAC,GAAG,KAAK,aAAa,CAAC,EAAE,OAAO,CAAC,aAAa,0BAA0B,KAAK,eAAe,QAAQ,CAAC,CAAC;AACpI,YAAM,mBAAmB,sBAAsB,OAAO,CAAC,aAAa;AAClE,cAAM,aAAa,KAAK,cAAc,QAAQ,GAAG,QAAQ;AACzD,eAAO,WAAW,SAAS,oBAAoB,KAAK,CAAC,WAAW,SAAS,oBAAoB;AAAA,MAC/F,CAAC;AACD,YAAM,2BAA2B,sBAAsB,KAAK,CAAC,aAAa,0BAA0B,KAAK,eAAe,QAAQ,CAAC,CAAC;AAClI,UAAI,iBAAiB,SAAS,KAAM,sBAAsB,SAAS,KAAK,CAAC,0BAA2B;AAClG,aAAK,MAAM,2FAA2F,sBAAsB,KAAK,IAAI,CAAC,EAAE;AAAA,MAC1I;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mgamadeus/ddd-web",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "TypeScript web client for backends built with mgamadeus/ddd, the DDD entity framework for Symfony and Doctrine (Composer/Packagist). Generates a typed RTK Query SDK from the backend's OpenAPI document, builds DDD QueryOptions (filters, orderBy, expand, select, paging) and handles JWT auth with token refresh.",
5
5
  "keywords": [
6
6
  "ddd",
@@ -45,6 +45,14 @@
45
45
  "types": "./dist/auth/index.d.ts",
46
46
  "import": "./dist/auth/index.js"
47
47
  },
48
+ "./fetch-api-request": {
49
+ "types": "./dist/fetch-api-request/index.d.ts",
50
+ "import": "./dist/fetch-api-request/index.js"
51
+ },
52
+ "./vite": {
53
+ "types": "./dist/vite/index.d.ts",
54
+ "import": "./dist/vite/index.js"
55
+ },
48
56
  "./codegen": {
49
57
  "types": "./dist/codegen/index.d.ts",
50
58
  "import": "./dist/codegen/index.js"