@smartlyio/oats-runtime 4.0.2 → 4.0.3

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,93 @@
1
+ import { Maker } from './make';
2
+ import * as runtime from './runtime';
3
+ export declare type Type = BinaryType | UnknownType | VoidType | BooleanType | IntegerType | NumberType | NullType | UnionType | IntersectionType | StringType | ArrayType | ObjectType | NamedType;
4
+ export declare type NamedTypeDefinitionDeferred<A, Shape = any> = () => NamedTypeDefinition<A, Shape>;
5
+ export interface NamedTypeDefinition<A, Shape = any> {
6
+ readonly name: string;
7
+ readonly definition: Type;
8
+ readonly maker: Maker<Shape, A>;
9
+ readonly isA: null | ((value: any) => value is A);
10
+ }
11
+ export interface BinaryType {
12
+ readonly type: 'binary';
13
+ }
14
+ export interface UnknownType {
15
+ readonly type: 'unknown';
16
+ }
17
+ export interface VoidType {
18
+ readonly type: 'void';
19
+ }
20
+ export interface NullType {
21
+ readonly type: 'null';
22
+ }
23
+ export interface UnionType {
24
+ readonly type: 'union';
25
+ readonly options: Type[];
26
+ }
27
+ export interface IntersectionType {
28
+ readonly type: 'intersection';
29
+ readonly options: Type[];
30
+ }
31
+ export interface StringType {
32
+ readonly type: 'string';
33
+ readonly format?: string;
34
+ readonly pattern?: string;
35
+ readonly minLength?: number;
36
+ readonly maxLength?: number;
37
+ readonly enum?: string[];
38
+ }
39
+ export interface BooleanType {
40
+ readonly type: 'boolean';
41
+ readonly enum?: boolean[];
42
+ }
43
+ export interface NumberType {
44
+ readonly type: 'number';
45
+ readonly enum?: number[];
46
+ readonly minimum?: number;
47
+ readonly maximum?: number;
48
+ }
49
+ export interface IntegerType {
50
+ readonly type: 'integer';
51
+ readonly enum?: number[];
52
+ readonly minimum?: number;
53
+ readonly maximum?: number;
54
+ }
55
+ export interface ArrayType {
56
+ readonly type: 'array';
57
+ readonly items: Type;
58
+ readonly minItems?: number;
59
+ readonly maxItems?: number;
60
+ }
61
+ export interface NamedType {
62
+ readonly type: 'named';
63
+ readonly reference: NamedTypeDefinitionDeferred<unknown>;
64
+ }
65
+ export interface PropType {
66
+ required: boolean;
67
+ value: Type;
68
+ }
69
+ export interface Props {
70
+ [name: string]: PropType;
71
+ }
72
+ export declare type AdditionalProp = boolean | Type;
73
+ export interface ObjectType {
74
+ readonly type: 'object';
75
+ readonly additionalProperties: AdditionalProp;
76
+ readonly properties: Props;
77
+ }
78
+ export declare class Traversal<Root, Leaf> {
79
+ private readonly root;
80
+ private readonly leaf;
81
+ static compile<Root, Leaf>(root: NamedTypeDefinition<Root>, leaf: NamedTypeDefinition<Leaf>): runtime.reflection.Traversal<Root, Leaf>;
82
+ private cache;
83
+ private cachedAncestors;
84
+ private constructor();
85
+ private dedupePaths;
86
+ map(value: Root, fn: (leaf: Leaf) => Leaf): Root;
87
+ pmap(value: Root, fn: (leaf: Leaf) => Promise<Leaf>): Promise<Root>;
88
+ private paths;
89
+ private matcher;
90
+ private validateRoot;
91
+ private addAncestor;
92
+ private ancestorNamedObjects;
93
+ }
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Traversal = void 0;
4
+ const assert = require("assert");
5
+ const safe_navigation_1 = require("@smartlyio/safe-navigation");
6
+ const runtime = require("./runtime");
7
+ class Traversal {
8
+ constructor(root, leaf) {
9
+ this.root = root;
10
+ this.leaf = leaf;
11
+ this.cache = new Map();
12
+ calculateReverseReach(new Set(), this.cache, this.root, this.leaf, false);
13
+ this.cachedAncestors = this.ancestorNamedObjects(this.leaf);
14
+ }
15
+ static compile(root, leaf) {
16
+ return new Traversal(root, leaf);
17
+ }
18
+ dedupePaths(ancestors) {
19
+ const deduped = new Map();
20
+ for (const [ancestor, paths] of ancestors.entries()) {
21
+ const dedupedPaths = paths.reduce((memo, path) => memo.add(JSON.stringify(path)), new Set());
22
+ deduped.set(ancestor, [...dedupedPaths.values()].map(path => JSON.parse(path)));
23
+ }
24
+ return deduped;
25
+ }
26
+ map(value, fn) {
27
+ this.validateRoot(value);
28
+ const match = this.matcher();
29
+ return runtime.map(value, ((value) => !!match(value)), (value) => {
30
+ this.paths(value).forEach((path) => {
31
+ value = safeMapPath(value, path, fn);
32
+ });
33
+ return value;
34
+ });
35
+ }
36
+ async pmap(value, fn) {
37
+ this.validateRoot(value);
38
+ const match = this.matcher();
39
+ return await runtime.pmap(value, ((value) => match(value)), async (value) => {
40
+ for (const path of this.paths(value)) {
41
+ value = await safePmapPath(value, path, fn);
42
+ }
43
+ return value;
44
+ });
45
+ }
46
+ paths(value) {
47
+ const paths = [];
48
+ for (const [ancestor, pathsFromAncestor] of this.cachedAncestors.entries()) {
49
+ if (ancestor.isA && ancestor.isA(value)) {
50
+ paths.push(...pathsFromAncestor);
51
+ }
52
+ }
53
+ return paths;
54
+ }
55
+ matcher() {
56
+ return (value) => {
57
+ for (const ancestor of this.cachedAncestors.keys()) {
58
+ if (ancestor.isA && ancestor.isA(value)) {
59
+ return true;
60
+ }
61
+ }
62
+ return false;
63
+ };
64
+ }
65
+ validateRoot(value) {
66
+ assert(this.root.isA && this.root.isA(value), 'Root value does not match expected root type');
67
+ }
68
+ addAncestor(ancestors, ancestor, path) {
69
+ const pathsToAncestor = ancestors.get(ancestor);
70
+ if (!pathsToAncestor) {
71
+ return ancestors.set(ancestor, [path]);
72
+ }
73
+ pathsToAncestor.push(path);
74
+ return ancestors;
75
+ }
76
+ ancestorNamedObjects(target) {
77
+ const found = this.cache.get(target);
78
+ if (!found) {
79
+ return assert.fail('no path to target');
80
+ }
81
+ const allAncestors = new Map();
82
+ for (const [pathStr, parents] of found.entries()) {
83
+ const path = JSON.parse(pathStr);
84
+ parents.forEach(ancestor => {
85
+ if (ancestor.isA) {
86
+ this.addAncestor(allAncestors, ancestor, path);
87
+ }
88
+ else if (['array', 'named'].indexOf(ancestor.definition.type) >= 0) {
89
+ for (const [namedObjectAncestor, paths] of this.ancestorNamedObjects(ancestor).entries()) {
90
+ paths.forEach(pathFromAncestor => this.addAncestor(allAncestors, namedObjectAncestor, [...pathFromAncestor, ...path]));
91
+ }
92
+ }
93
+ else {
94
+ assert.fail('nearest containing named thing is not an object: ' + ancestor.name);
95
+ }
96
+ });
97
+ }
98
+ return this.dedupePaths(allAncestors);
99
+ }
100
+ }
101
+ exports.Traversal = Traversal;
102
+ function safeMapPath(value, path, fn) {
103
+ let cursor = (0, safe_navigation_1.default)(value);
104
+ // tslint:disable-next-line:prefer-for-of
105
+ for (let ix = 0; ix < path.length; ix++) {
106
+ const p = path[ix];
107
+ if (p.type === 'array') {
108
+ return cursor.$map(arrayValue => arrayValue.map((item) => safeMapPath(item, path.slice(ix + 1), fn)));
109
+ }
110
+ if (p.type === 'additionalProperty') {
111
+ const fields = Object.keys(cursor.$ || {});
112
+ const extraFields = fields.filter(field => p.definedProperties.indexOf(field) < 0);
113
+ return cursor.$map((value) => {
114
+ for (const prop of extraFields) {
115
+ value = safeMapPath(value, [{ type: 'path', path: prop }, ...path.slice(ix + 1)], fn);
116
+ }
117
+ return value;
118
+ });
119
+ }
120
+ cursor = cursor[p.path];
121
+ }
122
+ return cursor.$map(fn);
123
+ }
124
+ async function safePmapPath(value, path, fn) {
125
+ let cursor = (0, safe_navigation_1.default)(value);
126
+ // tslint:disable-next-line:prefer-for-of
127
+ for (let ix = 0; ix < path.length; ix++) {
128
+ const p = path[ix];
129
+ if (p.type === 'array') {
130
+ return cursor.$pmap(arrayValue => Promise.all(arrayValue.map((item) => safePmapPath(item, path.slice(ix + 1), fn))));
131
+ }
132
+ if (p.type === 'additionalProperty') {
133
+ const fields = Object.keys(cursor.$ || {});
134
+ const extraFields = fields.filter(field => p.definedProperties.indexOf(field) < 0);
135
+ return cursor.$pmap(async (value) => {
136
+ for (const prop of extraFields) {
137
+ value = await safePmapPath(value, [{ type: 'path', path: prop }, ...path.slice(ix + 1)], fn);
138
+ }
139
+ return value;
140
+ });
141
+ }
142
+ cursor = cursor[p.path];
143
+ }
144
+ return cursor.$pmap(fn);
145
+ }
146
+ function canReach(reaches, from, to, byPath) {
147
+ let existing = reaches.get(to);
148
+ if (!existing) {
149
+ existing = new Map();
150
+ reaches.set(to, existing);
151
+ }
152
+ const pathString = JSON.stringify(byPath);
153
+ let froms = existing.get(pathString);
154
+ if (!froms) {
155
+ froms = [];
156
+ existing.set(pathString, froms);
157
+ }
158
+ froms.push(from);
159
+ }
160
+ function calculateReverseReach(processed, reaches, from, to, ambiguousPath) {
161
+ assert(from !== to || !ambiguousPath, 'Cannot calculate unambiguous type. There are union or intersection type between the nearest containing named object and the target leaf');
162
+ if (processed.has(from)) {
163
+ return;
164
+ }
165
+ if (from.definition.type === 'object') {
166
+ ambiguousPath = false;
167
+ processed.add(from);
168
+ }
169
+ calculateReachInType(processed, reaches, from, to, from.definition, [], ambiguousPath);
170
+ }
171
+ function ambiguousOptions(options) {
172
+ const ambiguous = options.filter(option => option.type !== 'null');
173
+ return ambiguous.length > 1;
174
+ }
175
+ function calculateReachInType(processed, reaches, from, to, type, path, ambiguousPath) {
176
+ if (type.type === 'named') {
177
+ canReach(reaches, from, type.reference(), path);
178
+ calculateReverseReach(processed, reaches, type.reference(), to, ambiguousPath);
179
+ }
180
+ else if (type.type === 'array') {
181
+ calculateReachInType(processed, reaches, from, to, type.items, [...path, { type: 'array' }], ambiguousPath);
182
+ }
183
+ else if (type.type === 'union') {
184
+ type.options.map(option => calculateReachInType(processed, reaches, from, to, option, path, ambiguousOptions(type.options) || ambiguousPath));
185
+ }
186
+ else if (type.type === 'intersection') {
187
+ type.options.map(option => calculateReachInType(processed, reaches, from, to, option, path, ambiguousOptions(type.options) || ambiguousPath));
188
+ }
189
+ else if (type.type === 'object') {
190
+ if (type.additionalProperties && type.additionalProperties !== true) {
191
+ calculateReachInType(processed, reaches, from, to, type.additionalProperties, [...path, { type: 'additionalProperty', definedProperties: Object.keys(type.properties) }], ambiguousPath);
192
+ }
193
+ Object.keys(type.properties).forEach(property => {
194
+ calculateReachInType(processed, reaches, from, to, type.properties[property].value, [...path, { type: 'path', path: property }], ambiguousPath);
195
+ });
196
+ }
197
+ }
198
+ //# sourceMappingURL=reflection-type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reflection-type.js","sourceRoot":"","sources":["../src/reflection-type.ts"],"names":[],"mappings":";;;AACA,iCAAiC;AACjC,gEAA8C;AAC9C,qCAAqC;AAmHrC,MAAa,SAAS;IAQpB,YACmB,IAA+B,EAC/B,IAA+B;QAD/B,SAAI,GAAJ,IAAI,CAA2B;QAC/B,SAAI,GAAJ,IAAI,CAA2B;QAL1C,UAAK,GAAY,IAAI,GAAG,EAAE,CAAC;QAOjC,qBAAqB,CAAC,IAAI,GAAG,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC1E,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC9D,CAAC;IAbD,MAAM,CAAC,OAAO,CAAa,IAA+B,EAAE,IAA+B;QACzF,OAAO,IAAI,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAaO,WAAW,CAAC,SAAoD;QACtE,MAAM,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QAC1B,KAAK,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,SAAS,CAAC,OAAO,EAAE,EAAE;YACnD,MAAM,YAAY,GAAgB,KAAK,CAAC,MAAM,CAC5C,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAC9C,IAAI,GAAG,EAAU,CAClB,CAAC;YACF,OAAO,CAAC,GAAG,CACT,QAAQ,EACR,CAAC,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAA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AAM,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE;QACjC,IAAI,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC,oBAAoB,KAAK,IAAI,EAAE;YACnE,oBAAoB,CAClB,SAAS,EACT,OAAO,EACP,IAAI,EACJ,EAAE,EACF,IAAI,CAAC,oBAAoB,EACzB,CAAC,GAAG,IAAI,EAAE,EAAE,IAAI,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,EAC1F,aAAa,CACd,CAAC;SACH;QACD,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YAC9C,oBAAoB,CAClB,SAAS,EACT,OAAO,EACP,IAAI,EACJ,EAAE,EACF,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,KAAK,EAC/B,CAAC,GAAG,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAC3C,aAAa,CACd,CAAC;QACJ,CAAC,CAAC,CAAC;KACJ;AACH,CAAC"}
@@ -0,0 +1,35 @@
1
+ import * as server from './server';
2
+ import * as client from './client';
3
+ import * as make from './make';
4
+ import { fromReflection } from './make';
5
+ import * as valueClass from './value-class';
6
+ import * as reflection from './reflection-type';
7
+ export { make, fromReflection, server, client, valueClass, reflection };
8
+ export declare const noContentContentType: "oatsNoContent";
9
+ declare type Scalar = number | string | boolean;
10
+ declare const typeWitnessKey: unique symbol;
11
+ declare const tagKey: unique symbol;
12
+ declare type ScalarWithoutBrand<V> = V extends ShapedClass<infer S> ? ScalarWithoutBrand<S> : V;
13
+ export declare type ShapeOf<A> = A extends Scalar ? ScalarWithoutBrand<A> : A extends ShapedClass<infer S> ? S : unknown extends A ? unknown : A extends Array<infer Item> ? Array<ShapeOf<Item>> : A extends ReadonlyArray<infer Item> ? ReadonlyArray<ShapeOf<Item>> : {
14
+ [K in keyof A]: ShapeOf<A[K]>;
15
+ };
16
+ declare class ShapedClass<Shape> {
17
+ protected [typeWitnessKey]: Shape;
18
+ }
19
+ declare class BrandedClass<Tag> {
20
+ protected [tagKey]: Tag;
21
+ }
22
+ export declare type Shaped<Type, Shape> = Type extends Nully ? Type : Type & ShapedClass<Shape>;
23
+ declare type Nully = null | undefined;
24
+ export declare type BrandedScalar<Type, Tag> = Type extends Nully ? Type : Shaped<Type, Type> & BrandedClass<Tag>;
25
+ export declare function setHeaders<Status extends number, ConntentType, Value, Headers extends Record<string, any>>(response: server.Response<Status, ConntentType, Value, Record<string, any>>, headers: Headers): server.Response<Status, ConntentType, Value, Headers>;
26
+ export declare function noContent<Status extends number>(status: Status): server.Response<Status, typeof noContentContentType, null, Record<string, any>>;
27
+ export declare function json<Status extends number, Value>(status: Status, value: Value): server.Response<Status, 'application/json', Value, Record<string, any>>;
28
+ export declare function text<Status extends number, Value>(status: Status, value: Value): server.Response<Status, 'text/plain', Value, Record<string, any>>;
29
+ export declare function set<Cls>(to: Cls, set: Cls extends valueClass.ValueClass ? Partial<ShapeOf<Cls>> : never): make.Make<Cls>;
30
+ declare type ValueType = valueClass.ValueClass | {
31
+ [key: string]: any;
32
+ } | readonly any[] | string | boolean | number;
33
+ export declare function map<A extends ValueType, T extends ValueType>(value: A, predicate: (a: any) => a is T, fn: (p: T, traversalPath: string[]) => T): A;
34
+ export declare function getAll<A extends ValueType, T extends ValueType>(value: A, predicate: (a: any) => a is T): readonly T[];
35
+ export declare function pmap<A extends ValueType, T extends ValueType>(value: A, predicate: (a: any) => a is T, map: (p: T, traversalPath: string[]) => Promise<T>): Promise<A>;
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pmap = exports.getAll = exports.map = exports.set = exports.text = exports.json = exports.noContent = exports.setHeaders = exports.noContentContentType = exports.reflection = exports.valueClass = exports.client = exports.server = exports.fromReflection = exports.make = void 0;
4
+ const server = require("./server");
5
+ exports.server = server;
6
+ const client = require("./client");
7
+ exports.client = client;
8
+ const make = require("./make");
9
+ exports.make = make;
10
+ const make_1 = require("./make");
11
+ Object.defineProperty(exports, "fromReflection", { enumerable: true, get: function () { return make_1.fromReflection; } });
12
+ const valueClass = require("./value-class");
13
+ exports.valueClass = valueClass;
14
+ const reflection = require("./reflection-type");
15
+ exports.reflection = reflection;
16
+ exports.noContentContentType = 'oatsNoContent';
17
+ const typeWitnessKey = Symbol();
18
+ const tagKey = Symbol();
19
+ class ShapedClass {
20
+ }
21
+ class BrandedClass {
22
+ }
23
+ function setHeaders(response, headers) {
24
+ return Object.assign(Object.assign({}, response), { headers });
25
+ }
26
+ exports.setHeaders = setHeaders;
27
+ function noContent(status) {
28
+ return {
29
+ status,
30
+ value: { contentType: exports.noContentContentType, value: null },
31
+ headers: {}
32
+ };
33
+ }
34
+ exports.noContent = noContent;
35
+ function json(status, value) {
36
+ return {
37
+ status,
38
+ value: { contentType: 'application/json', value },
39
+ headers: {}
40
+ };
41
+ }
42
+ exports.json = json;
43
+ function text(status, value) {
44
+ return {
45
+ status,
46
+ value: { contentType: 'text/plain', value },
47
+ headers: {}
48
+ };
49
+ }
50
+ exports.text = text;
51
+ function set(to, set) {
52
+ return to.constructor.make(Object.assign(Object.assign({}, to), set));
53
+ }
54
+ exports.set = set;
55
+ function map(value, predicate, fn) {
56
+ return mapInternal(value, predicate, fn, []);
57
+ }
58
+ exports.map = map;
59
+ function mapInternal(value, predicate, fn, traversalPath) {
60
+ if (predicate(value)) {
61
+ value = fn(value, traversalPath);
62
+ }
63
+ if (Array.isArray(value)) {
64
+ const arr = value.map((item, index) => mapInternal(item, predicate, fn, traversalPath.concat(String(index))));
65
+ return selectArray(value, arr);
66
+ }
67
+ if (value && typeof value === 'object') {
68
+ const record = {};
69
+ Object.keys(value).map(key => {
70
+ record[key] = mapInternal(value[key], predicate, fn, traversalPath.concat(key));
71
+ });
72
+ return selectRecord(value, record);
73
+ }
74
+ return value;
75
+ }
76
+ function getAll(value, predicate) {
77
+ return getAllInternal(value, predicate);
78
+ }
79
+ exports.getAll = getAll;
80
+ function getAllInternal(value, predicate) {
81
+ const match = [];
82
+ if (predicate(value)) {
83
+ match.push(value);
84
+ }
85
+ if (Array.isArray(value)) {
86
+ return [
87
+ ...match,
88
+ ...value.reduce((acc, item) => acc.concat(getAllInternal(item, predicate)), [])
89
+ ];
90
+ }
91
+ if (value && typeof value === 'object') {
92
+ return [
93
+ ...match,
94
+ ...Object.values(value).reduce((acc, item) => acc.concat(getAllInternal(item, predicate)), [])
95
+ ];
96
+ }
97
+ return match;
98
+ }
99
+ async function pmap(value, predicate, map) {
100
+ return pmapInternal(value, predicate, map, []);
101
+ }
102
+ exports.pmap = pmap;
103
+ function isPromise(p) {
104
+ return p && typeof p.then === 'function';
105
+ }
106
+ function pmapInternal(value, predicate, map, traversalPath) {
107
+ if (predicate(value)) {
108
+ value = map(value, traversalPath);
109
+ }
110
+ if (isPromise(value)) {
111
+ return value.then(n => pmapComposite(n, predicate, map, traversalPath));
112
+ }
113
+ return pmapComposite(value, predicate, map, traversalPath);
114
+ }
115
+ function selectArray(original, newArray) {
116
+ for (let i = 0; i < original.length; i++) {
117
+ if (original[i] !== newArray[i]) {
118
+ return newArray;
119
+ }
120
+ }
121
+ return original.length !== newArray.length ? newArray : original;
122
+ }
123
+ function selectRecord(original, newRecord) {
124
+ const changed = Object.keys(original).some(key => {
125
+ return original[key] !== newRecord[key];
126
+ });
127
+ if (!changed) {
128
+ return original;
129
+ }
130
+ if (original instanceof valueClass.ValueClass) {
131
+ return set(original, newRecord).success();
132
+ }
133
+ return newRecord;
134
+ }
135
+ function pmapArray(value, predicate, map, traversalPath) {
136
+ const mapped = value.map((n, i) => pmapInternal(n, predicate, map, traversalPath.concat(String(i))));
137
+ if (mapped.some(isPromise)) {
138
+ return Promise.all(mapped).then(newValues => {
139
+ return selectArray(value, newValues);
140
+ });
141
+ }
142
+ return selectArray(value, mapped);
143
+ }
144
+ function pmapObject(value, predicate, map, traversalPath) {
145
+ const record = {};
146
+ const promises = [];
147
+ Object.keys(value).forEach(key => {
148
+ const v = pmapInternal(value[key], predicate, map, traversalPath.concat(key));
149
+ if (isPromise(v)) {
150
+ promises.push(v.then(result => {
151
+ record[key] = result;
152
+ }));
153
+ }
154
+ else {
155
+ record[key] = v;
156
+ }
157
+ });
158
+ if (promises.length) {
159
+ return Promise.all(promises).then(() => selectRecord(value, record));
160
+ }
161
+ return selectRecord(value, record);
162
+ }
163
+ function pmapComposite(value, predicate, map, traversalPath) {
164
+ if (Array.isArray(value)) {
165
+ return pmapArray(value, predicate, map, traversalPath);
166
+ }
167
+ if (value && typeof value === 'object') {
168
+ return pmapObject(value, predicate, map, traversalPath);
169
+ }
170
+ return value;
171
+ }
172
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":";;;AAAA,mCAAmC;AAOJ,wBAAM;AANrC,mCAAmC;AAMI,wBAAM;AAL7C,+BAA+B;AAKtB,oBAAI;AAJb,iCAAwC;AAIzB,+FAJN,qBAAc,OAIM;AAH7B,4CAA4C;AAGG,gCAAU;AAFzD,gDAAgD;AAEW,gCAAU;AAExD,QAAA,oBAAoB,GAAG,eAAwB,CAAC;AAI7D,MAAM,cAAc,GAAG,MAAM,EAAE,CAAC;AAChC,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC;AAgBxB,MAAM,WAAW;CAEhB;AAED,MAAM,YAAY;CAEjB;AAUD,SAAgB,UAAU,CAMxB,QAA2E,EAC3E,OAAgB;IAEhB,uCAAY,QAAQ,KAAE,OAAO,IAAG;AAClC,CAAC;AAVD,gCAUC;AAED,SAAgB,SAAS,CACvB,MAAc;IAEd,OAAO;QACL,MAAM;QACN,KAAK,EAAE,EAAE,WAAW,EAAE,4BAAoB,EAAE,KAAK,EAAE,IAAI,EAAE;QACzD,OAA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,MAAM,CAAC,GAAG,YAAY,CAAE,KAAa,CAAC,GAAG,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACvF,IAAI,SAAS,CAAC,CAAC,CAAC,EAAE;YAChB,QAAQ,CAAC,IAAI,CACX,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;gBACd,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;YACvB,CAAC,CAAC,CACH,CAAC;SACH;aAAM;YACL,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;SACjB;IACH,CAAC,CAAC,CAAC;IAEH,IAAI,QAAQ,CAAC,MAAM,EAAE;QACnB,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;KACtE;IACD,OAAO,YAAY,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACrC,CAAC;AAED,SAAS,aAAa,CACpB,KAAQ,EACR,SAA6B,EAC7B,GAAkD,EAClD,aAAuB;IAEvB,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACxB,OAAO,SAAS,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,aAAa,CAAQ,CAAC;KAC/D;IACD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACtC,OAAO,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,aAAa,CAAC,CAAC;KACzD;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,84 @@
1
+ import { MakeOptions, Maker, ValidationError } from './make';
2
+ export interface Response<Status extends number, ContentType, Value, Headers extends Record<string, any>> {
3
+ status: Status;
4
+ value: {
5
+ contentType: ContentType;
6
+ value: Value;
7
+ };
8
+ headers: Headers;
9
+ }
10
+ export interface RequestBody<A> {
11
+ contentType: string;
12
+ value: A;
13
+ }
14
+ export declare type Params = object;
15
+ export declare type Headers = object;
16
+ export declare type Query = object;
17
+ export declare type RequestContext = any;
18
+ export interface EndpointArg<H extends Headers | void, P extends Params | void, Q extends Query | void, Body extends RequestBody<any> | void> {
19
+ path: string;
20
+ method: Methods;
21
+ servers: string[];
22
+ op?: string;
23
+ headers: H;
24
+ params: P;
25
+ query: Q;
26
+ body: Body;
27
+ }
28
+ export declare type ServerEndpointArg<H extends Headers | void, P extends Params | void, Q extends Query | void, Body extends RequestBody<any> | void, RC extends RequestContext> = EndpointArg<H, P, Q, Body> & {
29
+ readonly requestContext: RC;
30
+ };
31
+ export declare type Endpoint<H extends Headers | void, P extends Params | void, Q extends Query | void, Body extends RequestBody<any> | void, R extends Response<number, any, any, Record<string, any>>, RC extends RequestContext> = (ctx: ServerEndpointArg<H, P, Q, Body, RC>) => Promise<R>;
32
+ export declare type SafeEndpoint = Endpoint<Headers | undefined, Params | undefined, Query | undefined, RequestBody<any> | undefined, Response<number, any, any, Record<string, any>>, RequestContext>;
33
+ export interface MethodHandlers {
34
+ get?: SafeEndpoint;
35
+ post?: SafeEndpoint;
36
+ put?: SafeEndpoint;
37
+ delete?: SafeEndpoint;
38
+ patch?: SafeEndpoint;
39
+ options?: SafeEndpoint;
40
+ head?: SafeEndpoint;
41
+ }
42
+ export interface Endpoints {
43
+ [opOrUrl: string]: MethodHandlers;
44
+ }
45
+ export declare class RequestValidationError extends Error {
46
+ tag: string;
47
+ errors: ValidationError[];
48
+ constructor(tag: string, errors: ValidationError[]);
49
+ }
50
+ export declare class ResponseValidationError extends Error {
51
+ tag: string;
52
+ originalResponse: any;
53
+ errors: ValidationError[];
54
+ constructor(tag: string, originalResponse: any, errors: ValidationError[]);
55
+ }
56
+ export declare function safe<H extends Headers, P extends Params, Q extends Query, Body extends RequestBody<any>, R extends Response<any, any, any, Record<string, any>>, RC extends RequestContext>(headers: Maker<any, H>, params: Maker<any, P>, query: Maker<any, Q>, body: Maker<any, Body>, response: Maker<any, R>, endpoint: Endpoint<H, P, Q, Body, R, RC>, { validationOptions }?: HandlerOptions): Endpoint<Headers, Params, Query, RequestBody<any>, Response<number, any, any, Record<string, any>>, RequestContext>;
57
+ export declare type Methods = keyof MethodHandlers;
58
+ export declare const supportedMethods: Methods[];
59
+ declare type AnyMaker = Maker<any, any>;
60
+ export interface Handler {
61
+ op?: string;
62
+ path: string;
63
+ servers: string[];
64
+ method: Methods;
65
+ headers: AnyMaker;
66
+ query: AnyMaker;
67
+ body: AnyMaker;
68
+ params: AnyMaker;
69
+ response: AnyMaker;
70
+ }
71
+ export declare type HandlerFactory<Spec> = (adapter: ServerAdapter) => (spec: Spec) => void;
72
+ export declare function assertMethod(method: string): Methods;
73
+ export declare type ServerAdapter = (path: string, op: string, method: Methods, handler: SafeEndpoint, servers: string[]) => void;
74
+ export interface HandlerOptions {
75
+ /**
76
+ * Options for request schema validation.
77
+ */
78
+ validationOptions?: {
79
+ query?: MakeOptions;
80
+ params?: MakeOptions;
81
+ };
82
+ }
83
+ export declare function createHandlerFactory<Spec>(handlers: Handler[], opts?: HandlerOptions): HandlerFactory<Spec>;
84
+ export {};