@mfeflow/url-factory 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +39 -2
- package/dist/index.d.ts +46 -0
- package/dist/index.js +33 -0
- package/package.json +53 -3
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
|
-
#
|
|
1
|
+
# @mfeflow/url-factory
|
|
2
2
|
|
|
3
|
-
|
|
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
type IdChar = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '_';
|
|
2
|
+
/** The leading letters, digits and underscores of `S`: where `fillPath` ends a param name. */
|
|
3
|
+
type Ident<S extends string, Acc extends string = ''> = S extends `${infer C}${infer Rest}` ? C extends IdChar ? Ident<Rest, `${Acc}${C}`> : Acc : Acc;
|
|
4
|
+
/**
|
|
5
|
+
* Names of `:params` in a path template, e.g. `'/v1/orders/:orderId/items/:itemId'` gives
|
|
6
|
+
* `'orderId' | 'itemId'`. A name is letters, digits and underscores, as in `fillPath`, so
|
|
7
|
+
* `'/a/:order-id'` has the param `order`.
|
|
8
|
+
*/
|
|
9
|
+
export type PathParams<P extends string> = string extends P ? string : P extends `${string}:${infer Rest}` ? Exclude<Ident<Rest>, ''> | PathParams<Rest> : never;
|
|
10
|
+
/**
|
|
11
|
+
* Fills the `:params` of a path template with encoded values. Throws for a missing param and for
|
|
12
|
+
* the values `.` and `..`, which would walk out of the path. `scope` and `id` only name the caller in
|
|
13
|
+
* the message.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fillPath(template: string, params: Readonly<Record<string, string | number>> | undefined, scope: string, id: string): string;
|
|
16
|
+
export type QueryValue = string | number | boolean | null | undefined | readonly (string | number | boolean)[];
|
|
17
|
+
export type Query = Readonly<Record<string, QueryValue>>;
|
|
18
|
+
export interface Endpoint {
|
|
19
|
+
/** Key into the runtime config's service base URLs, e.g. `orders`. */
|
|
20
|
+
readonly service: string;
|
|
21
|
+
/** Path template relative to the service base, e.g. `/v1/orders/:orderId`. */
|
|
22
|
+
readonly path: string;
|
|
23
|
+
}
|
|
24
|
+
export type Endpoints = Readonly<Record<string, Endpoint>>;
|
|
25
|
+
/** `[options?]` for paths without params, `[options]` with required `params` otherwise. */
|
|
26
|
+
export type UrlArgs<P extends string> = [PathParams<P>] extends [never] ? [options?: {
|
|
27
|
+
query?: Query;
|
|
28
|
+
}] : [options: {
|
|
29
|
+
params: Readonly<Record<PathParams<P>, string | number>>;
|
|
30
|
+
query?: Query;
|
|
31
|
+
}];
|
|
32
|
+
export type UrlFactory<E extends Endpoints> = <K extends keyof E & string>(key: K, ...args: UrlArgs<E[K]['path']>) => string;
|
|
33
|
+
/** Identity helper that keeps the literal path types, so params are checked. */
|
|
34
|
+
export declare function defineEndpoints<const E extends Endpoints>(endpoints: E): E;
|
|
35
|
+
/**
|
|
36
|
+
* Builds absolute URLs for `endpoints`. `baseUrl(service)` is called on every
|
|
37
|
+
* call, so it can read runtime config that loads after the factory is created.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* const endpoints = defineEndpoints({ getOrder: { service: 'orders', path: '/v1/orders/:orderId' } });
|
|
41
|
+
* const apiUrl = createUrlFactory(endpoints, (s) => getRuntimeConfig().services[s]);
|
|
42
|
+
* apiUrl('getOrder', { params: { orderId: 42 }, query: { expand: 'items' } });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function createUrlFactory<E extends Endpoints>(endpoints: E, baseUrl: (service: E[keyof E]['service']) => string | undefined): UrlFactory<E>;
|
|
46
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const PARAM = /:([A-Za-z0-9_]+)/g;
|
|
2
|
+
function fillPath(template, params, scope, id) {
|
|
3
|
+
return template.replace(PARAM, (_match, name)=>{
|
|
4
|
+
const value = params?.[name];
|
|
5
|
+
if (void 0 === value) throw new Error(`${scope}: missing param "${name}" for "${id}"`);
|
|
6
|
+
const text = String(value);
|
|
7
|
+
if ('.' === text || '..' === text) throw new Error(`${scope}: param "${name}" for "${id}" cannot be "${text}"`);
|
|
8
|
+
return encodeURIComponent(text);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function defineEndpoints(endpoints) {
|
|
12
|
+
for (const [key, e] of Object.entries(endpoints))if (!e.path.startsWith('/')) throw new Error(`Endpoint "${key}": path must start with "/"`);
|
|
13
|
+
return endpoints;
|
|
14
|
+
}
|
|
15
|
+
function appendQuery(url, query) {
|
|
16
|
+
if (!query) return;
|
|
17
|
+
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));
|
|
18
|
+
else url.searchParams.append(name, String(value));
|
|
19
|
+
}
|
|
20
|
+
function createUrlFactory(endpoints, baseUrl) {
|
|
21
|
+
return (key, ...args)=>{
|
|
22
|
+
const endpoint = endpoints[key];
|
|
23
|
+
if (!endpoint) throw new Error(`url-factory: unknown endpoint "${key}"`);
|
|
24
|
+
const base = baseUrl(endpoint.service);
|
|
25
|
+
if (!base) throw new Error(`url-factory: no base URL for service "${endpoint.service}" (endpoint "${key}")`);
|
|
26
|
+
const options = args[0];
|
|
27
|
+
const path = fillPath(endpoint.path, options?.params, 'url-factory', key);
|
|
28
|
+
const url = new URL(path.slice(1), base.endsWith('/') ? base : `${base}/`);
|
|
29
|
+
appendQuery(url, options?.query);
|
|
30
|
+
return url.toString();
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export { createUrlFactory, defineEndpoints, fillPath };
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mfeflow/url-factory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.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
|
}
|