@yrest/cli 0.8.1 → 0.10.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/README.md +195 -45
- package/dist/cli/index.js +1241 -209
- package/dist/cli/index.mjs +1238 -206
- package/dist/index.d.mts +91 -5
- package/dist/index.d.ts +91 -5
- package/dist/index.js +814 -188
- package/dist/index.mjs +811 -185
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -4,23 +4,107 @@ import * as http from 'http';
|
|
|
4
4
|
|
|
5
5
|
/** A single REST resource item. Field names and value types are user-defined in the YAML file. */
|
|
6
6
|
type Resource = Record<string, unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Descriptor for a single field declared in the `_schema` block.
|
|
9
|
+
*
|
|
10
|
+
* All properties are optional — omit what you don't need. Fields not mentioned
|
|
11
|
+
* in `_schema` are inferred from the collection data and treated as optional.
|
|
12
|
+
*
|
|
13
|
+
* Shorthand: `fieldName: required` normalises to `{ required: true }`.
|
|
14
|
+
*/
|
|
15
|
+
type FieldDef = {
|
|
16
|
+
/** If `true`, the field is listed in the OpenAPI `required` array and (Phase B) validated on POST/PUT. */
|
|
17
|
+
required?: boolean;
|
|
18
|
+
/** Explicit type override. If absent, inferred from the data. */
|
|
19
|
+
type?: "string" | "integer" | "number" | "boolean" | "object" | "array";
|
|
20
|
+
/** OpenAPI `format` hint (e.g. `email`, `date`, `uuid`, `uri`, `date-time`). */
|
|
21
|
+
format?: string;
|
|
22
|
+
/** Restricts the field to a fixed set of values. */
|
|
23
|
+
enum?: unknown[];
|
|
24
|
+
/** Human-readable description included in the OpenAPI spec. */
|
|
25
|
+
description?: string;
|
|
26
|
+
/** Default value included in the OpenAPI spec. */
|
|
27
|
+
default?: unknown;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Field-level schema declarations for one or more collections, parsed from `_schema` in the YAML.
|
|
31
|
+
*
|
|
32
|
+
* - Outer key: collection name.
|
|
33
|
+
* - Inner key: field name within that collection.
|
|
34
|
+
* - Value: {@link FieldDef} descriptor (or the string shorthand `"required"` / `"optional"`).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```yaml
|
|
38
|
+
* _schema:
|
|
39
|
+
* users:
|
|
40
|
+
* name: required # shorthand
|
|
41
|
+
* email:
|
|
42
|
+
* required: true
|
|
43
|
+
* format: email
|
|
44
|
+
* age:
|
|
45
|
+
* type: integer
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
type SchemaBlock = Record<string, Record<string, FieldDef>>;
|
|
7
49
|
/**
|
|
8
50
|
* The full in-memory database.
|
|
9
51
|
* Keys are collection names (e.g. `"users"`); values are arrays of {@link Resource} items.
|
|
10
52
|
*/
|
|
11
53
|
type Data = Record<string, Resource[]>;
|
|
12
54
|
/**
|
|
13
|
-
*
|
|
55
|
+
* Canonical descriptor for a single relation declared under `_rel`.
|
|
56
|
+
*
|
|
57
|
+
* The YAML accepts two forms that both normalise to this type:
|
|
58
|
+
* - **Shorthand string** — `userId: users` → `{ type: "many2one", target: "users" }`
|
|
59
|
+
* - **Object** — explicit `type` plus the fields required by that type.
|
|
60
|
+
*
|
|
61
|
+
* ### Types
|
|
62
|
+
* - `many2one` — (default) many children share one parent via a FK field.
|
|
63
|
+
* - `one2one` — one child belongs to one parent; `?_embed` returns a single object, not an array.
|
|
64
|
+
* - `many2many` — join through a pivot collection; the relation key is the embed alias.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // _rel:
|
|
68
|
+
* // posts:
|
|
69
|
+
* // userId: users ← shorthand many2one
|
|
70
|
+
* // tags:
|
|
71
|
+
* // type: many2many
|
|
72
|
+
* // through: post_tags
|
|
73
|
+
* // foreignKey: postId
|
|
74
|
+
* // otherKey: tagId
|
|
75
|
+
* // users:
|
|
76
|
+
* // profileId:
|
|
77
|
+
* // type: one2one
|
|
78
|
+
* // target: profiles
|
|
79
|
+
*/
|
|
80
|
+
type RelationDef = {
|
|
81
|
+
type: "many2one";
|
|
82
|
+
target: string;
|
|
83
|
+
nested?: boolean;
|
|
84
|
+
} | {
|
|
85
|
+
type: "one2one";
|
|
86
|
+
target: string;
|
|
87
|
+
nested?: boolean;
|
|
88
|
+
} | {
|
|
89
|
+
type: "many2many";
|
|
90
|
+
target: string;
|
|
91
|
+
through: string;
|
|
92
|
+
foreignKey: string;
|
|
93
|
+
otherKey: string;
|
|
94
|
+
nested?: boolean;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Relational mappings declared under `_rel` in the YAML file, normalised to {@link RelationDef}.
|
|
14
98
|
*
|
|
15
|
-
* - Outer key:
|
|
16
|
-
* - Inner key:
|
|
17
|
-
* - Inner value:
|
|
99
|
+
* - Outer key: source collection name (child for many2one/one2one; either side for many2many).
|
|
100
|
+
* - Inner key: FK field name (many2one/one2one) or embed alias (many2many).
|
|
101
|
+
* - Inner value: canonical {@link RelationDef}.
|
|
18
102
|
*
|
|
19
103
|
* @example
|
|
20
104
|
* // Given: _rel: { posts: { userId: users } }
|
|
21
105
|
* // GET /users/1/posts → returns posts where userId === "1"
|
|
22
106
|
*/
|
|
23
|
-
type Relations = Record<string, Record<string,
|
|
107
|
+
type Relations = Record<string, Record<string, RelationDef>>;
|
|
24
108
|
/**
|
|
25
109
|
* A static response block shared by {@link CustomRoute} and {@link Scenario}.
|
|
26
110
|
*/
|
|
@@ -138,6 +222,8 @@ interface YrestStorage {
|
|
|
138
222
|
getData(): Data;
|
|
139
223
|
/** Returns the relational mappings declared under `_rel`. */
|
|
140
224
|
getRelations(): Relations;
|
|
225
|
+
/** Returns the field-level schema declarations from `_schema`, or `{}` if absent. */
|
|
226
|
+
getSchema(): SchemaBlock;
|
|
141
227
|
/**
|
|
142
228
|
* Returns the items in a named collection, or `undefined` if it does not exist.
|
|
143
229
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -4,23 +4,107 @@ import * as http from 'http';
|
|
|
4
4
|
|
|
5
5
|
/** A single REST resource item. Field names and value types are user-defined in the YAML file. */
|
|
6
6
|
type Resource = Record<string, unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Descriptor for a single field declared in the `_schema` block.
|
|
9
|
+
*
|
|
10
|
+
* All properties are optional — omit what you don't need. Fields not mentioned
|
|
11
|
+
* in `_schema` are inferred from the collection data and treated as optional.
|
|
12
|
+
*
|
|
13
|
+
* Shorthand: `fieldName: required` normalises to `{ required: true }`.
|
|
14
|
+
*/
|
|
15
|
+
type FieldDef = {
|
|
16
|
+
/** If `true`, the field is listed in the OpenAPI `required` array and (Phase B) validated on POST/PUT. */
|
|
17
|
+
required?: boolean;
|
|
18
|
+
/** Explicit type override. If absent, inferred from the data. */
|
|
19
|
+
type?: "string" | "integer" | "number" | "boolean" | "object" | "array";
|
|
20
|
+
/** OpenAPI `format` hint (e.g. `email`, `date`, `uuid`, `uri`, `date-time`). */
|
|
21
|
+
format?: string;
|
|
22
|
+
/** Restricts the field to a fixed set of values. */
|
|
23
|
+
enum?: unknown[];
|
|
24
|
+
/** Human-readable description included in the OpenAPI spec. */
|
|
25
|
+
description?: string;
|
|
26
|
+
/** Default value included in the OpenAPI spec. */
|
|
27
|
+
default?: unknown;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Field-level schema declarations for one or more collections, parsed from `_schema` in the YAML.
|
|
31
|
+
*
|
|
32
|
+
* - Outer key: collection name.
|
|
33
|
+
* - Inner key: field name within that collection.
|
|
34
|
+
* - Value: {@link FieldDef} descriptor (or the string shorthand `"required"` / `"optional"`).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```yaml
|
|
38
|
+
* _schema:
|
|
39
|
+
* users:
|
|
40
|
+
* name: required # shorthand
|
|
41
|
+
* email:
|
|
42
|
+
* required: true
|
|
43
|
+
* format: email
|
|
44
|
+
* age:
|
|
45
|
+
* type: integer
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
type SchemaBlock = Record<string, Record<string, FieldDef>>;
|
|
7
49
|
/**
|
|
8
50
|
* The full in-memory database.
|
|
9
51
|
* Keys are collection names (e.g. `"users"`); values are arrays of {@link Resource} items.
|
|
10
52
|
*/
|
|
11
53
|
type Data = Record<string, Resource[]>;
|
|
12
54
|
/**
|
|
13
|
-
*
|
|
55
|
+
* Canonical descriptor for a single relation declared under `_rel`.
|
|
56
|
+
*
|
|
57
|
+
* The YAML accepts two forms that both normalise to this type:
|
|
58
|
+
* - **Shorthand string** — `userId: users` → `{ type: "many2one", target: "users" }`
|
|
59
|
+
* - **Object** — explicit `type` plus the fields required by that type.
|
|
60
|
+
*
|
|
61
|
+
* ### Types
|
|
62
|
+
* - `many2one` — (default) many children share one parent via a FK field.
|
|
63
|
+
* - `one2one` — one child belongs to one parent; `?_embed` returns a single object, not an array.
|
|
64
|
+
* - `many2many` — join through a pivot collection; the relation key is the embed alias.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // _rel:
|
|
68
|
+
* // posts:
|
|
69
|
+
* // userId: users ← shorthand many2one
|
|
70
|
+
* // tags:
|
|
71
|
+
* // type: many2many
|
|
72
|
+
* // through: post_tags
|
|
73
|
+
* // foreignKey: postId
|
|
74
|
+
* // otherKey: tagId
|
|
75
|
+
* // users:
|
|
76
|
+
* // profileId:
|
|
77
|
+
* // type: one2one
|
|
78
|
+
* // target: profiles
|
|
79
|
+
*/
|
|
80
|
+
type RelationDef = {
|
|
81
|
+
type: "many2one";
|
|
82
|
+
target: string;
|
|
83
|
+
nested?: boolean;
|
|
84
|
+
} | {
|
|
85
|
+
type: "one2one";
|
|
86
|
+
target: string;
|
|
87
|
+
nested?: boolean;
|
|
88
|
+
} | {
|
|
89
|
+
type: "many2many";
|
|
90
|
+
target: string;
|
|
91
|
+
through: string;
|
|
92
|
+
foreignKey: string;
|
|
93
|
+
otherKey: string;
|
|
94
|
+
nested?: boolean;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Relational mappings declared under `_rel` in the YAML file, normalised to {@link RelationDef}.
|
|
14
98
|
*
|
|
15
|
-
* - Outer key:
|
|
16
|
-
* - Inner key:
|
|
17
|
-
* - Inner value:
|
|
99
|
+
* - Outer key: source collection name (child for many2one/one2one; either side for many2many).
|
|
100
|
+
* - Inner key: FK field name (many2one/one2one) or embed alias (many2many).
|
|
101
|
+
* - Inner value: canonical {@link RelationDef}.
|
|
18
102
|
*
|
|
19
103
|
* @example
|
|
20
104
|
* // Given: _rel: { posts: { userId: users } }
|
|
21
105
|
* // GET /users/1/posts → returns posts where userId === "1"
|
|
22
106
|
*/
|
|
23
|
-
type Relations = Record<string, Record<string,
|
|
107
|
+
type Relations = Record<string, Record<string, RelationDef>>;
|
|
24
108
|
/**
|
|
25
109
|
* A static response block shared by {@link CustomRoute} and {@link Scenario}.
|
|
26
110
|
*/
|
|
@@ -138,6 +222,8 @@ interface YrestStorage {
|
|
|
138
222
|
getData(): Data;
|
|
139
223
|
/** Returns the relational mappings declared under `_rel`. */
|
|
140
224
|
getRelations(): Relations;
|
|
225
|
+
/** Returns the field-level schema declarations from `_schema`, or `{}` if absent. */
|
|
226
|
+
getSchema(): SchemaBlock;
|
|
141
227
|
/**
|
|
142
228
|
* Returns the items in a named collection, or `undefined` if it does not exist.
|
|
143
229
|
*
|