@mfeflow/url-factory 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sanjeev Sharma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,40 @@
1
- # Temporary Holding Version
1
+ # @mfeflow/url-factory
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Typed API URLs for micro-frontends. Path params are checked from the template at compile time, and base URLs are read from runtime config on every call, so one build runs in every environment.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @mfeflow/url-factory
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { createUrlFactory, defineEndpoints } from '@mfeflow/url-factory';
15
+
16
+ const endpoints = defineEndpoints({
17
+ getOrder: { service: 'orders', path: '/v1/orders/:orderId' },
18
+ listOrders: { service: 'orders', path: '/v1/orders' },
19
+ });
20
+
21
+ const services: Record<string, string> = { orders: 'https://api.example.com' };
22
+ const apiUrl = createUrlFactory(endpoints, (service) => services[service]);
23
+
24
+ apiUrl('getOrder', { params: { orderId: 42 }, query: { expand: 'items' } });
25
+ // https://api.example.com/v1/orders/42?expand=items
26
+ apiUrl('getOrder'); // compile error: params are required
27
+ ```
28
+
29
+ A missing param, an unknown endpoint or a service without a base URL throws with a message that names it.
30
+
31
+ ## Module Federation sharing
32
+
33
+ Bucket: **stateless**. Nothing here holds state shared between micro-frontends, so each remote can bundle its own copy and different versions can run side by side. No sharing config is needed.
34
+
35
+ ## Links
36
+
37
+ - [mfeflow repository](https://github.com/mfeflow/mfeflow) and its README
38
+ - [Documentation](https://mfeflow.github.io/mfeflow/)
39
+
40
+ MIT licensed.
@@ -0,0 +1,32 @@
1
+ /** Names of `:params` in a path template, e.g. `'/v1/orders/:orderId/items/:itemId'` → `'orderId' | 'itemId'`. */
2
+ export type PathParams<P extends string> = P extends `${string}:${infer Param}/${infer Rest}` ? Param | PathParams<`/${Rest}`> : P extends `${string}:${infer Param}` ? Param : never;
3
+ export type QueryValue = string | number | boolean | null | undefined | readonly (string | number | boolean)[];
4
+ export type Query = Readonly<Record<string, QueryValue>>;
5
+ export interface Endpoint {
6
+ /** Key into the runtime config's service base URLs, e.g. `orders`. */
7
+ readonly service: string;
8
+ /** Path template relative to the service base, e.g. `/v1/orders/:orderId`. */
9
+ readonly path: string;
10
+ }
11
+ export type Endpoints = Readonly<Record<string, Endpoint>>;
12
+ /** `[options?]` for paths without params, `[options]` with required `params` otherwise. */
13
+ export type UrlArgs<P extends string> = [PathParams<P>] extends [never] ? [options?: {
14
+ query?: Query;
15
+ }] : [options: {
16
+ params: Readonly<Record<PathParams<P>, string | number>>;
17
+ query?: Query;
18
+ }];
19
+ export type UrlFactory<E extends Endpoints> = <K extends keyof E & string>(key: K, ...args: UrlArgs<E[K]['path']>) => string;
20
+ /** Identity helper that keeps the literal path types, so params are checked. */
21
+ export declare function defineEndpoints<const E extends Endpoints>(endpoints: E): E;
22
+ /**
23
+ * Builds absolute URLs for `endpoints`. `baseUrl(service)` is called on every
24
+ * call, so it can read runtime config that loads after the factory is created.
25
+ *
26
+ * ```ts
27
+ * const endpoints = defineEndpoints({ getOrder: { service: 'orders', path: '/v1/orders/:orderId' } });
28
+ * const apiUrl = createUrlFactory(endpoints, (s) => getRuntimeConfig().services[s]);
29
+ * apiUrl('getOrder', { params: { orderId: 42 }, query: { expand: 'items' } });
30
+ * ```
31
+ */
32
+ export declare function createUrlFactory<E extends Endpoints>(endpoints: E, baseUrl: (service: E[keyof E]['service']) => string | undefined): UrlFactory<E>;
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ function defineEndpoints(endpoints) {
2
+ for (const [key, e] of Object.entries(endpoints))if (!e.path.startsWith('/')) throw new Error(`Endpoint "${key}": path must start with "/"`);
3
+ return endpoints;
4
+ }
5
+ function appendQuery(url, query) {
6
+ if (!query) return;
7
+ for (const [name, value] of Object.entries(query))if (null != value) if (Array.isArray(value)) for (const item of value)url.searchParams.append(name, String(item));
8
+ else url.searchParams.append(name, String(value));
9
+ }
10
+ function createUrlFactory(endpoints, baseUrl) {
11
+ return (key, ...args)=>{
12
+ const endpoint = endpoints[key];
13
+ if (!endpoint) throw new Error(`url-factory: unknown endpoint "${key}"`);
14
+ const base = baseUrl(endpoint.service);
15
+ if (!base) throw new Error(`url-factory: no base URL for service "${endpoint.service}" (endpoint "${key}")`);
16
+ const options = args[0];
17
+ const path = endpoint.path.replace(/:([A-Za-z0-9_]+)/g, (_match, name)=>{
18
+ const value = options?.params?.[name];
19
+ if (void 0 === value) throw new Error(`url-factory: missing param "${name}" for "${key}"`);
20
+ return encodeURIComponent(String(value));
21
+ });
22
+ const url = new URL(path.slice(1), base.endsWith('/') ? base : `${base}/`);
23
+ appendQuery(url, options?.query);
24
+ return url.toString();
25
+ };
26
+ }
27
+ export { createUrlFactory, defineEndpoints };
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@mfeflow/url-factory",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Typed API URLs for micro-frontends: path params are checked from the template at compile time, base URLs come from runtime config, so one build runs in every environment.",
5
+ "keywords": [
6
+ "micro-frontend",
7
+ "mfe",
8
+ "module-federation",
9
+ "url",
10
+ "typescript",
11
+ "api",
12
+ "runtime-config",
13
+ "react",
14
+ "rsbuild"
15
+ ],
16
+ "homepage": "https://mfeflow.github.io/mfeflow/",
17
+ "bugs": "https://github.com/mfeflow/mfeflow/issues",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/mfeflow/mfeflow.git",
21
+ "directory": "packages/url-factory"
22
+ },
23
+ "license": "MIT",
24
+ "author": "Sanjeev Sharma <speedsharma.ai@gmail.com>",
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "engines": {
38
+ "node": ">=22.12"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public",
42
+ "provenance": true
43
+ },
44
+ "devDependencies": {
45
+ "@rslib/core": "1.0.5",
46
+ "@types/node": "26.6.6",
47
+ "typescript": "7.0.2"
48
+ },
49
+ "mfeflow": {
50
+ "bucket": "stateless"
51
+ },
52
+ "scripts": {
53
+ "build": "rslib build",
54
+ "typecheck": "tsc --noEmit"
55
+ }
6
56
  }