@kravc/dos 1.10.4 → 1.10.6
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/package.json +2 -2
- package/src/Document.js +12 -4
- package/src/Document.spec.js +7 -0
- package/src/Service.js +1 -1
- package/src/Service.spec.js +9 -0
- package/src/helpers/createSchemasMap.js +10 -1
- package/src/index.d.ts +91 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kravc/dos",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.6",
|
|
4
4
|
"description": "Convention-based, easy-to-use library for building API-driven serverless services.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Service",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"author": "Alexander Kravets <a@kra.vc>",
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@kravc/schema": "^2.7.
|
|
28
|
+
"@kravc/schema": "^2.7.3",
|
|
29
29
|
"cookie": "^1.0.1",
|
|
30
30
|
"js-yaml": "^4.1.0",
|
|
31
31
|
"jsonwebtoken": "^9.0.2",
|
package/src/Document.js
CHANGED
|
@@ -161,10 +161,6 @@ class Document extends Component {
|
|
|
161
161
|
return { objects, ...rest }
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
static async indexAll(context, query, options) {
|
|
165
|
-
return this.index(context, query, options)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
164
|
static _index(query) {
|
|
169
165
|
const filter = item =>
|
|
170
166
|
Object.keys(query).every(key => item[key] === query[key])
|
|
@@ -177,6 +173,18 @@ class Document extends Component {
|
|
|
177
173
|
return { items, count: items.length }
|
|
178
174
|
}
|
|
179
175
|
|
|
176
|
+
static async indexAll(context, query = {}, options = {}) {
|
|
177
|
+
let { items, ...rest } = await this._indexAll(query, options)
|
|
178
|
+
|
|
179
|
+
const objects = items.map(item => new this(context, item))
|
|
180
|
+
|
|
181
|
+
return { objects, ...rest }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static _indexAll(query) {
|
|
185
|
+
return this._index(query)
|
|
186
|
+
}
|
|
187
|
+
|
|
180
188
|
static async update(context, query, mutation, originalDocument = null) {
|
|
181
189
|
mutation = omit(mutation, [ this.idKey, 'createdAt', 'createdBy' ])
|
|
182
190
|
|
package/src/Document.spec.js
CHANGED
|
@@ -183,6 +183,9 @@ describe('Document', () => {
|
|
|
183
183
|
|
|
184
184
|
result = await Profile.indexAll(context, { name: 'Dasha' })
|
|
185
185
|
expect(result.count).to.eql(1)
|
|
186
|
+
|
|
187
|
+
result = await Profile.indexAll(context)
|
|
188
|
+
expect(result.count).to.eql(3)
|
|
186
189
|
})
|
|
187
190
|
})
|
|
188
191
|
|
|
@@ -240,6 +243,10 @@ describe('Document', () => {
|
|
|
240
243
|
const callbacks = {}
|
|
241
244
|
|
|
242
245
|
class CustomProfile extends Profile {
|
|
246
|
+
static getPartition() {
|
|
247
|
+
return 'CustomProfile'
|
|
248
|
+
}
|
|
249
|
+
|
|
243
250
|
static beforeCreate(context, mutation) {
|
|
244
251
|
callbacks.beforeCreate = mutation
|
|
245
252
|
}
|
package/src/Service.js
CHANGED
package/src/Service.spec.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { Schema } = require('@kravc/schema')
|
|
4
|
+
|
|
3
5
|
const Health = require('examples/Health')
|
|
4
6
|
const { expect } = require('chai')
|
|
5
7
|
const ReadProfile = require('examples/ReadProfile')
|
|
@@ -16,7 +18,14 @@ const {
|
|
|
16
18
|
Component,
|
|
17
19
|
} = require('src')
|
|
18
20
|
|
|
21
|
+
const testSchema = new Schema({
|
|
22
|
+
test: {
|
|
23
|
+
type: 'string'
|
|
24
|
+
}
|
|
25
|
+
}, 'Test')
|
|
26
|
+
|
|
19
27
|
const modules = [
|
|
28
|
+
testSchema,
|
|
20
29
|
Health,
|
|
21
30
|
ReadProfile,
|
|
22
31
|
CreateProfile,
|
|
@@ -5,6 +5,8 @@ const { keyBy } = require('lodash')
|
|
|
5
5
|
const loadSync = require('./loadSync')
|
|
6
6
|
const { readdirSync, statSync } = require('fs')
|
|
7
7
|
|
|
8
|
+
const SCHEMA_CLASS_NAME = 'Schema'
|
|
9
|
+
|
|
8
10
|
const listFilesSync = dir =>
|
|
9
11
|
readdirSync(dir)
|
|
10
12
|
.reduce((files, file) =>
|
|
@@ -18,10 +20,17 @@ const readSchemasSync = path =>
|
|
|
18
20
|
.filter(fileName => fileName.endsWith('.yaml'))
|
|
19
21
|
.map(schemaPath => loadSync(schemaPath))
|
|
20
22
|
|
|
21
|
-
const createSchemasMap = path => {
|
|
23
|
+
const createSchemasMap = (path, modules) => {
|
|
22
24
|
const yamlSchemas = readSchemasSync(path)
|
|
23
25
|
const schemasMap = keyBy(yamlSchemas, 'id')
|
|
24
26
|
|
|
27
|
+
const schemas = modules
|
|
28
|
+
.filter(item => item.constructor.name === SCHEMA_CLASS_NAME)
|
|
29
|
+
|
|
30
|
+
for (const schema of schemas) {
|
|
31
|
+
schemasMap[schema.id] = schema
|
|
32
|
+
}
|
|
33
|
+
|
|
25
34
|
return schemasMap
|
|
26
35
|
}
|
|
27
36
|
|
package/src/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Schema, SchemaAttributes } from '@kravc/schema';
|
|
1
2
|
|
|
2
3
|
export declare type SortOrder = 'asc' | 'desc';
|
|
3
4
|
export declare type Context = Record<string, any>;
|
|
@@ -30,6 +31,9 @@ export declare class Document<T> {
|
|
|
30
31
|
static get idKeyPrefix(): string;
|
|
31
32
|
static get documentName(): string;
|
|
32
33
|
|
|
34
|
+
static set schema(schema: Schema): void;
|
|
35
|
+
static get schema(): Schema;
|
|
36
|
+
|
|
33
37
|
static _read(query: QueryMap): Promise<AttributesMap>;
|
|
34
38
|
static _create(attributes: AttributesMap): Promise<Boolean>;
|
|
35
39
|
static _update(query: QueryMap, mutation: UpdateMutationMap): Promise<AttributesMap>;
|
|
@@ -149,3 +153,90 @@ export declare function verifyToken(
|
|
|
149
153
|
publicKey: string,
|
|
150
154
|
algorithm: string,
|
|
151
155
|
): Promise<[ boolean, string | undefined ]>;
|
|
156
|
+
|
|
157
|
+
export declare class JwtAuthorization {
|
|
158
|
+
static createRequirement(options: {
|
|
159
|
+
publicKey: string;
|
|
160
|
+
name?: string;
|
|
161
|
+
publicKey?: string;
|
|
162
|
+
algorithm?: string;
|
|
163
|
+
cookieName?: string;
|
|
164
|
+
normalizePayload?: Function;
|
|
165
|
+
tokenVerificationMethod?: Function;
|
|
166
|
+
accessVerificationMethod?: Function;
|
|
167
|
+
}): Record<string, any>
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export declare class SystemAuthorization {
|
|
171
|
+
static createRequirement(options?: {
|
|
172
|
+
name?: string;
|
|
173
|
+
} = {}): Record<string, any>
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
type ComponentConstructor = new (...args: any[]) => any;
|
|
177
|
+
type OperationConstructor = new (...args: any[]) => any;
|
|
178
|
+
|
|
179
|
+
export declare class Operation {
|
|
180
|
+
public context: Context;
|
|
181
|
+
static get query(): SchemaAttributes | null;
|
|
182
|
+
static get mutation(): Schema | SchemaAttributes | null;
|
|
183
|
+
static get output(): Schema | SchemaAttributes | null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export declare function Read(
|
|
187
|
+
Component: ComponentConstructor,
|
|
188
|
+
componentAction?: string
|
|
189
|
+
): typeof Operation;
|
|
190
|
+
|
|
191
|
+
export declare function Create(
|
|
192
|
+
Component: ComponentConstructor,
|
|
193
|
+
componentAction?: string
|
|
194
|
+
): typeof Operation;
|
|
195
|
+
|
|
196
|
+
export declare function Update(
|
|
197
|
+
Component: ComponentConstructor,
|
|
198
|
+
componentAction?: string
|
|
199
|
+
): typeof Operation;
|
|
200
|
+
|
|
201
|
+
export declare function Delete(
|
|
202
|
+
Component: ComponentConstructor,
|
|
203
|
+
componentAction?: string
|
|
204
|
+
): typeof Operation;
|
|
205
|
+
|
|
206
|
+
export declare function Index(
|
|
207
|
+
Component: ComponentConstructor,
|
|
208
|
+
componentAction?: string
|
|
209
|
+
): typeof Operation;
|
|
210
|
+
|
|
211
|
+
declare type Module = Operation | Schema | ComponentConstructor;
|
|
212
|
+
|
|
213
|
+
interface ISpec {
|
|
214
|
+
paths: Record<string, unknown>;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
interface IRequest {
|
|
218
|
+
url: string;
|
|
219
|
+
body?: string | Record<string, unknown>;
|
|
220
|
+
method: string;
|
|
221
|
+
headers: Record<string, any>;
|
|
222
|
+
operationId?: string;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
interface IResponse {
|
|
226
|
+
body?: string,
|
|
227
|
+
headers: Record<string, string>,
|
|
228
|
+
statusCode: number,
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export declare class Service {
|
|
232
|
+
constructor(modules: Module[], options: {
|
|
233
|
+
url?: string;
|
|
234
|
+
path?: string;
|
|
235
|
+
skipOperations?: string[];
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
get spec(): ISpec;
|
|
239
|
+
get baseUrl(): string;
|
|
240
|
+
|
|
241
|
+
async handler(request: IRequest, Context): Promise<IResponse>;
|
|
242
|
+
}
|