@feathersjs/schema 5.0.0-pre.20 → 5.0.0-pre.23

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/src/schema.ts CHANGED
@@ -1,60 +1,60 @@
1
- import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv';
2
- import { FromSchema, JSONSchema } from 'json-schema-to-ts';
3
- import { BadRequest } from '@feathersjs/errors';
1
+ import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv'
2
+ import { FromSchema, JSONSchema } from 'json-schema-to-ts'
3
+ import { BadRequest } from '@feathersjs/errors'
4
4
 
5
5
  export const DEFAULT_AJV = new Ajv({
6
6
  coerceTypes: true
7
- });
7
+ })
8
8
 
9
- export { Ajv };
9
+ export { Ajv }
10
10
 
11
11
  export type JSONSchemaDefinition = JSONSchema & {
12
- $id: string,
13
- $async?: boolean,
12
+ $id: string
13
+ $async?: boolean
14
14
  properties?: { [key: string]: JSONSchema }
15
15
  required?: readonly string[]
16
- };
16
+ }
17
17
 
18
18
  export interface Schema<T> {
19
- validate <X = T> (...args: Parameters<ValidateFunction<X>>): Promise<X>;
19
+ validate<X = T>(...args: Parameters<ValidateFunction<X>>): Promise<X>
20
20
  }
21
21
 
22
22
  export class SchemaWrapper<S extends JSONSchemaDefinition> implements Schema<FromSchema<S>> {
23
- ajv: Ajv;
24
- validator: AsyncValidateFunction;
25
- readonly _type!: FromSchema<S>;
23
+ ajv: Ajv
24
+ validator: AsyncValidateFunction
25
+ readonly _type!: FromSchema<S>
26
26
 
27
- constructor (public definition: S, ajv: Ajv = DEFAULT_AJV) {
28
- this.ajv = ajv;
27
+ constructor(public definition: S, ajv: Ajv = DEFAULT_AJV) {
28
+ this.ajv = ajv
29
29
  this.validator = this.ajv.compile({
30
30
  $async: true,
31
31
  ...(this.definition as any)
32
- }) as AsyncValidateFunction;
32
+ }) as AsyncValidateFunction
33
33
  }
34
34
 
35
- get properties () {
36
- return this.definition.properties as S['properties'];
35
+ get properties() {
36
+ return this.definition.properties as S['properties']
37
37
  }
38
38
 
39
- get required () {
40
- return this.definition.required as S['required'];
39
+ get required() {
40
+ return this.definition.required as S['required']
41
41
  }
42
42
 
43
- async validate <T = FromSchema<S>> (...args: Parameters<ValidateFunction<T>>) {
43
+ async validate<T = FromSchema<S>>(...args: Parameters<ValidateFunction<T>>) {
44
44
  try {
45
- const validated = await this.validator(...args) as T;
45
+ const validated = (await this.validator(...args)) as T
46
46
 
47
- return validated;
47
+ return validated
48
48
  } catch (error: any) {
49
- throw new BadRequest(error.message, error.errors);
49
+ throw new BadRequest(error.message, error.errors)
50
50
  }
51
51
  }
52
52
 
53
- toJSON () {
54
- return this.definition;
53
+ toJSON() {
54
+ return this.definition
55
55
  }
56
56
  }
57
57
 
58
- export function schema <S extends JSONSchemaDefinition> (definition: S, ajv: Ajv = DEFAULT_AJV) {
59
- return new SchemaWrapper(definition, ajv);
58
+ export function schema<S extends JSONSchemaDefinition>(definition: S, ajv: Ajv = DEFAULT_AJV) {
59
+ return new SchemaWrapper(definition, ajv)
60
60
  }
package/lib/hooks.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import { HookContext, NextFunction } from '@feathersjs/feathers';
2
- import { Resolver } from './resolver';
3
- import { Schema } from './schema';
4
- export declare const resolveQuery: <T>(...resolvers: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>[]) => (context: HookContext, next?: NextFunction) => Promise<any>;
5
- export declare const resolveData: <T>(...resolvers: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>[]) => (context: HookContext, next?: NextFunction) => Promise<any>;
6
- export declare const resolveResult: <T>(...resolvers: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>[]) => (context: HookContext, next?: NextFunction) => Promise<void>;
7
- export declare const validateQuery: (schema: Schema<any>) => (context: HookContext, next?: NextFunction) => Promise<any>;
8
- export declare const validateData: (schema: Schema<any>) => (context: HookContext, next?: NextFunction) => Promise<any>;
package/lib/hooks.js DELETED
@@ -1,119 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateData = exports.validateQuery = exports.resolveResult = exports.resolveData = exports.resolveQuery = void 0;
4
- const lib_1 = require("../../errors/lib");
5
- const getContext = (context) => {
6
- return {
7
- ...context,
8
- params: {
9
- ...context.params,
10
- query: {}
11
- }
12
- };
13
- };
14
- const runResolvers = async (resolvers, data, ctx, status) => {
15
- let current = data;
16
- for (const resolver of resolvers) {
17
- current = await resolver.resolve(current, ctx, status);
18
- }
19
- return current;
20
- };
21
- const resolveQuery = (...resolvers) => async (context, next) => {
22
- var _a;
23
- const ctx = getContext(context);
24
- const data = ((_a = context === null || context === void 0 ? void 0 : context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
25
- const query = await runResolvers(resolvers, data, ctx);
26
- context.params = {
27
- ...context.params,
28
- query
29
- };
30
- if (typeof next === 'function') {
31
- return next();
32
- }
33
- };
34
- exports.resolveQuery = resolveQuery;
35
- const resolveData = (...resolvers) => async (context, next) => {
36
- const ctx = getContext(context);
37
- const data = context.data;
38
- const status = {
39
- originalContext: context
40
- };
41
- if (Array.isArray(data)) {
42
- context.data = await Promise.all(data.map(current => runResolvers(resolvers, current, ctx, status)));
43
- }
44
- else {
45
- context.data = await runResolvers(resolvers, data, ctx, status);
46
- }
47
- if (typeof next === 'function') {
48
- return next();
49
- }
50
- };
51
- exports.resolveData = resolveData;
52
- const resolveResult = (...resolvers) => async (context, next) => {
53
- var _a;
54
- if (typeof next === 'function') {
55
- const { $resolve: properties, ...query } = ((_a = context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
56
- const resolve = {
57
- originalContext: context,
58
- ...context.params.resolve,
59
- properties
60
- };
61
- context.params = {
62
- ...context.params,
63
- resolve,
64
- query
65
- };
66
- await next();
67
- }
68
- const ctx = getContext(context);
69
- const status = context.params.resolve;
70
- const isPaginated = context.method === 'find' && context.result.data;
71
- const data = isPaginated ? context.result.data : context.result;
72
- const result = Array.isArray(data) ?
73
- await Promise.all(data.map(async (current) => runResolvers(resolvers, current, ctx, status))) :
74
- await runResolvers(resolvers, data, ctx, status);
75
- if (isPaginated) {
76
- context.result.data = result;
77
- }
78
- else {
79
- context.result = result;
80
- }
81
- };
82
- exports.resolveResult = resolveResult;
83
- const validateQuery = (schema) => async (context, next) => {
84
- var _a;
85
- const data = ((_a = context === null || context === void 0 ? void 0 : context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
86
- try {
87
- const query = await schema.validate(data);
88
- context.params = {
89
- ...context.params,
90
- query
91
- };
92
- if (typeof next === 'function') {
93
- return next();
94
- }
95
- }
96
- catch (error) {
97
- throw (error.ajv ? new lib_1.BadRequest(error.message, error.errors) : error);
98
- }
99
- };
100
- exports.validateQuery = validateQuery;
101
- const validateData = (schema) => async (context, next) => {
102
- const data = context.data;
103
- try {
104
- if (Array.isArray(data)) {
105
- context.data = await Promise.all(data.map(current => schema.validate(current)));
106
- }
107
- else {
108
- context.data = await schema.validate(data);
109
- }
110
- }
111
- catch (error) {
112
- throw (error.ajv ? new lib_1.BadRequest(error.message, error.errors) : error);
113
- }
114
- if (typeof next === 'function') {
115
- return next();
116
- }
117
- };
118
- exports.validateData = validateData;
119
- //# sourceMappingURL=hooks.js.map
package/lib/hooks.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":";;;AACA,0CAA8C;AAI9C,MAAM,UAAU,GAAG,CAAC,OAAoB,EAAE,EAAE;IAC1C,OAAO;QACL,GAAG,OAAO;QACV,MAAM,EAAE;YACN,GAAG,OAAO,CAAC,MAAM;YACjB,KAAK,EAAE,EAAE;SACV;KACF,CAAA;AACH,CAAC,CAAA;AAED,MAAM,YAAY,GAAG,KAAK,EACxB,SAAqC,EACrC,IAAS,EACT,GAAgB,EAChB,MAAgD,EAChD,EAAE;IACF,IAAI,OAAO,GAAQ,IAAI,CAAC;IAExB,KAAK,MAAM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IAAI;QACF,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YACvB,OAAO,CAAC,IAAI,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAClD,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CACzB,CAAC,CAAC;SACJ;aAAM;YACL,OAAO,CAAC,IAAI,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;SAC5C;KACF;IAAC,OAAO,KAAU,EAAE;QACnB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,gBAAU,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;KACzE;IAED,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;QAC9B,OAAO,IAAI,EAAE,CAAC;KACf;AACH,CAAC,CAAC;AAnBS,QAAA,YAAY,gBAmBrB"}
package/src/hooks.ts DELETED
@@ -1,143 +0,0 @@
1
- import { HookContext, NextFunction } from '@feathersjs/feathers';
2
- import { BadRequest } from '../../errors/lib';
3
- import { Resolver, ResolverStatus } from './resolver';
4
- import { Schema } from './schema';
5
-
6
- const getContext = (context: HookContext) => {
7
- return {
8
- ...context,
9
- params: {
10
- ...context.params,
11
- query: {}
12
- }
13
- }
14
- }
15
-
16
- const runResolvers = async <T> (
17
- resolvers: Resolver<T, HookContext>[],
18
- data: any,
19
- ctx: HookContext,
20
- status?: Partial<ResolverStatus<T, HookContext>>
21
- ) => {
22
- let current: any = data;
23
-
24
- for (const resolver of resolvers) {
25
- current = await resolver.resolve(current, ctx, status);
26
- }
27
-
28
- return current as T;
29
- }
30
-
31
- export const resolveQuery = <T> (...resolvers: Resolver<T, HookContext>[]) =>
32
- async (context: HookContext, next?: NextFunction) => {
33
- const ctx = getContext(context);
34
- const data = context?.params?.query || {};
35
- const query = await runResolvers(resolvers, data, ctx);
36
-
37
- context.params = {
38
- ...context.params,
39
- query
40
- }
41
-
42
- if (typeof next === 'function') {
43
- return next();
44
- }
45
- };
46
-
47
- export const resolveData = <T> (...resolvers: Resolver<T, HookContext>[]) =>
48
- async (context: HookContext, next?: NextFunction) => {
49
- const ctx = getContext(context);
50
- const data = context.data;
51
- const status = {
52
- originalContext: context
53
- };
54
-
55
- if (Array.isArray(data)) {
56
- context.data = await Promise.all(data.map(current =>
57
- runResolvers(resolvers, current, ctx, status)
58
- ));
59
- } else {
60
- context.data = await runResolvers(resolvers, data, ctx, status);
61
- }
62
-
63
- if (typeof next === 'function') {
64
- return next();
65
- }
66
- };
67
-
68
- export const resolveResult = <T> (...resolvers: Resolver<T, HookContext>[]) =>
69
- async (context: HookContext, next?: NextFunction) => {
70
- if (typeof next === 'function') {
71
- const { $resolve: properties, ...query } = context.params?.query || {};
72
- const resolve = {
73
- originalContext: context,
74
- ...context.params.resolve,
75
- properties
76
- };
77
-
78
- context.params = {
79
- ...context.params,
80
- resolve,
81
- query
82
- }
83
-
84
- await next();
85
- }
86
-
87
- const ctx = getContext(context);
88
- const status = context.params.resolve;
89
-
90
- const isPaginated = context.method === 'find' && context.result.data;
91
- const data = isPaginated ? context.result.data : context.result;
92
-
93
- const result = Array.isArray(data) ?
94
- await Promise.all(data.map(async current => runResolvers(resolvers, current, ctx, status))) :
95
- await runResolvers(resolvers, data, ctx, status);
96
-
97
- if (isPaginated) {
98
- context.result.data = result;
99
- } else {
100
- context.result = result;
101
- }
102
- };
103
-
104
- export const validateQuery = (schema: Schema<any>) =>
105
- async (context: HookContext, next?: NextFunction) => {
106
- const data = context?.params?.query || {};
107
-
108
- try {
109
- const query = await schema.validate(data);
110
-
111
- context.params = {
112
- ...context.params,
113
- query
114
- }
115
-
116
- if (typeof next === 'function') {
117
- return next();
118
- }
119
- } catch (error: any) {
120
- throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
121
- }
122
- };
123
-
124
- export const validateData = (schema: Schema<any>) =>
125
- async (context: HookContext, next?: NextFunction) => {
126
- const data = context.data;
127
-
128
- try {
129
- if (Array.isArray(data)) {
130
- context.data = await Promise.all(data.map(current =>
131
- schema.validate(current)
132
- ));
133
- } else {
134
- context.data = await schema.validate(data);
135
- }
136
- } catch (error: any) {
137
- throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
138
- }
139
-
140
- if (typeof next === 'function') {
141
- return next();
142
- }
143
- };