@cap-js/cds-types 0.2.0 → 0.3.0-beta.1

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.
@@ -1,449 +0,0 @@
1
- /* eslint-disable @typescript-eslint/ban-types */
2
- import { SELECT, INSERT, UPDATE, DELETE, Query, ConstructedQuery, UPSERT } from './ql'
3
- import { Awaitable } from './ql'
4
- import { ArrayConstructable, Constructable } from './internal/inference'
5
- import { LinkedCSN, LinkedDefinition, Definitions as LinkedDefinitions, LinkedEntity } from './linked'
6
- import { CSN } from './csn'
7
- import { EventContext } from './events'
8
- import { Request } from './events'
9
- import { ReadableStream } from 'node:stream/web'
10
-
11
- type Key = number | string | any
12
-
13
- export class QueryAPI {
14
-
15
- entities: LinkedCSN['entities']
16
-
17
- /**
18
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
19
- */
20
- read: {
21
- <T extends ArrayConstructable>(entity: T, key?: Key): Awaitable<SELECT<T>, InstanceType<T>>,
22
- <T>(entity: LinkedDefinition | string, key?: Key): SELECT<T>,
23
- }
24
-
25
- /**
26
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
27
- */
28
- create: {
29
- <T extends ArrayConstructable>(entity: T, key?: Key): INSERT<T>,
30
- <T>(entity: LinkedDefinition | string, key?: Key): INSERT<T>,
31
- }
32
-
33
- /**
34
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
35
- */
36
- insert: {
37
- <T extends ArrayConstructable>(data: T): INSERT<T>,
38
- <T>(data: object | object[]): INSERT<T>,
39
- }
40
-
41
- /**
42
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
43
- */
44
- upsert: {
45
- <T extends ArrayConstructable>(data: T): UPSERT<T>,
46
- <T>(data: object | object[]): UPSERT<T>,
47
- }
48
-
49
- /**
50
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
51
- */
52
- update: {
53
- <T extends ArrayConstructable>(entity: T, key?: Key): UPDATE<T>,
54
- <T>(entity: LinkedDefinition | string, key?: Key): UPDATE<T>,
55
- }
56
-
57
- /**
58
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
59
- */
60
- run: {
61
- (query: ConstructedQuery | ConstructedQuery[]): Promise<ResultSet | any>,
62
- (query: Query): Promise<ResultSet | any>,
63
- (query: string, args?: any[] | object): Promise<ResultSet | any>,
64
- }
65
-
66
- /**
67
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#srv-stream-column)
68
- */
69
- stream: {
70
- (column: string): {
71
- from(entity: LinkedDefinition | string): {
72
- where(filter: any): ReadableStream,
73
- },
74
- },
75
- (query: Query): Promise<ReadableStream>,
76
- }
77
-
78
- /**
79
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
80
- */
81
- delete<T>(entity: LinkedDefinition | string, key?: Key): DELETE<T>
82
-
83
- /**
84
- * @see [docs](https://cap.cloud.sap/docs/node.js/core-services#srv-foreach-entity)
85
- */
86
- foreach (query: Query, callback: (row: object) => void): this
87
- transaction: {
88
- (fn: (tx: Transaction) => object): Promise<any>,
89
- (context?: object): Transaction,
90
- (context: object, fn: (tx: Transaction) => object): Promise<any>,
91
- }
92
-
93
- }
94
-
95
-
96
- /**
97
- * Class cds.Service
98
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
99
- */
100
- export class Service extends QueryAPI {
101
-
102
- constructor (
103
- name?: string,
104
- model?: CSN,
105
- options?: {
106
- kind: string,
107
- impl: string | ServiceImpl,
108
- }
109
- )
110
-
111
- /**
112
- * The kind of the service
113
- */
114
- kind: string
115
-
116
- /**
117
- * The name of the service
118
- */
119
- name: string
120
-
121
- /**
122
- * The model from which the service's definition was loaded
123
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
124
- */
125
- model: LinkedCSN
126
-
127
- /**
128
- * Provides access to the entities exposed by a service
129
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
130
- */
131
- entities: LinkedDefinitions & ((namespace: string) => LinkedDefinitions)
132
-
133
- /**
134
- * Provides access to the events declared by a service
135
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
136
- */
137
- events: LinkedDefinitions & ((namespace: string) => LinkedDefinitions)
138
-
139
- /**
140
- * Provides access to the types exposed by a service
141
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
142
- */
143
- types: LinkedDefinitions & ((namespace: string) => LinkedDefinitions)
144
-
145
- /**
146
- * Provides access to the operations, i.e. actions and functions, exposed by a service
147
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
148
- */
149
- operations: LinkedDefinitions & ((namespace: string) => LinkedDefinitions)
150
-
151
- /**
152
- * Acts like a parameter-less constructor. Ensure to call `await super.init()` to have the base class’s handlers added.
153
- * You may register own handlers before the base class’s ones, to intercept requests before the default handlers snap in.
154
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#srv-init)
155
- */
156
- init (): Promise<void>
157
-
158
- /**
159
- * Constructs and emits an asynchronous event.
160
- * @see [capire docs](https://cap.cloud.sap/docs/core-services#srv-emit-event)
161
- */
162
- emit: {
163
- <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T>,
164
- <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
165
- }
166
-
167
- /**
168
- * Constructs and sends a synchronous request.
169
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#srv-send-request)
170
- */
171
- send: {
172
- <T = any>(event: types.event, path: string, data?: object, headers?: object): Promise<T>,
173
- <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
174
- <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T>,
175
- <T = any>(details: { query: ConstructedQuery, data?: object, headers?: object }): Promise<T>,
176
- <T = any>(details: { method: types.eventName, path: string, data?: object, headers?: object }): Promise<T>,
177
- <T = any>(details: { event: types.eventName, entity: LinkedDefinition | string, data?: object, params?: object, headers?: object }): Promise<T>,
178
- }
179
-
180
- /**
181
- * Constructs and sends a GET request.
182
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#rest-style-api)
183
- */
184
- get<T = any>(entityOrPath: types.target, data?: object): Promise<T>
185
-
186
- /**
187
- * Constructs and sends a POST request.
188
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#rest-style-api)
189
- */
190
- post<T = any>(entityOrPath: types.target, data?: object): Promise<T>
191
-
192
- /**
193
- * Constructs and sends a PUT request.
194
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#rest-style-api)
195
- */
196
- put<T = any>(entityOrPath: types.target, data?: object): Promise<T>
197
-
198
- /**
199
- * Constructs and sends a PATCH request.
200
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#rest-style-api)
201
- */
202
- patch<T = any>(entityOrPath: types.target, data?: object): Promise<T>
203
-
204
- /**
205
- * Constructs and sends a DELETE request.
206
- */
207
- delete: {
208
- <T = any>(entityOrPath: types.target, data?: object): DELETE<T>,
209
- <T extends ArrayConstructable>(entity: T, key?: Key): DELETE<T>,
210
- <T>(entity: LinkedDefinition | string, key?: Key): DELETE<T>,
211
- }
212
-
213
- // The central method to dispatch events
214
- dispatch (msg: types.event): Promise<any>
215
-
216
- // FIXME: not yet documented, will come in future version
217
- // disconnect (tenant?: string): Promise<void>
218
-
219
- // Provider API
220
- prepend (fn: ServiceImpl): Promise<this>
221
-
222
- on<T extends Constructable>(eve: types.event, entity: T, handler: CRUDEventHandler.On<InstanceType<T>, InstanceType<T> | void | Error>): this
223
-
224
- on<F extends CdsFunction>(boundAction: F, service: string, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
225
-
226
- on<F extends CdsFunction>(unboundAction: F, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
227
-
228
- on (eve: types.event, entity: types.target, handler: OnEventHandler): this
229
-
230
- on (eve: types.event, handler: OnEventHandler): this
231
-
232
- on (eve: 'error', handler: OnErrorHandler): this
233
-
234
-
235
- // onSucceeded (eve: types.Events, entity: types.Target, handler: types.EventHandler): this
236
- // onSucceeded (eve: types.Events, handler: types.EventHandler): this
237
- // onFailed (eve: types.Events, entity: types.Target, handler: types.EventHandler): this
238
- // onFailed (eve: types.Events, handler: types.EventHandler): this
239
- before<T extends Constructable>(eve: types.event, entity: T, handler: CRUDEventHandler.Before<InstanceType<T>, InstanceType<T> | void | Error>): this
240
-
241
- before (eve: types.event, entity: types.target, handler: EventHandler): this
242
-
243
- before (eve: types.event, handler: EventHandler): this
244
-
245
- after<T extends Constructable>(eve: types.event, entity: T, handler: CRUDEventHandler.After<InstanceType<T>, InstanceType<T> | void | Error>): this
246
-
247
- after (eve: types.event, entity: types.target, handler: ResultsHandler): this
248
-
249
- after (eve: types.event, handler: ResultsHandler): this
250
-
251
- reject (eves: types.event, ...entity: types.target[]): this
252
-
253
- }
254
-
255
- export class ApplicationService extends Service {}
256
- export class MessagingService extends Service {}
257
- export class RemoteService extends Service {}
258
- export class DatabaseService extends Service {
259
-
260
- deploy (model?: CSN | string): Promise<CSN>
261
-
262
- begin (): Promise<void>
263
-
264
- commit (): Promise<void>
265
-
266
- rollback (): Promise<void>
267
-
268
- }
269
-
270
-
271
- export default class cds {
272
-
273
-
274
- /**
275
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services)
276
- */
277
- Service: typeof Service
278
-
279
- /**
280
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/app-services)
281
- */
282
- ApplicationService: typeof ApplicationService
283
-
284
- /**
285
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/remote-services)
286
- */
287
- RemoteService: typeof RemoteService
288
-
289
- /**
290
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/messaging)
291
- */
292
- MessagingService: typeof MessagingService
293
-
294
- /**
295
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/databases)
296
- */
297
- DatabaseService: typeof DatabaseService
298
-
299
- }
300
-
301
-
302
- export interface Transaction extends Service {
303
- commit(): Promise<void>
304
- rollback(): Promise<void>
305
- }
306
-
307
- interface ResultSet extends Array<object> {}
308
-
309
- interface ServiceImpl {
310
- (this: Service, srv: Service): any
311
- }
312
-
313
- interface EventHandler {
314
- // (msg : types.EventMessage) : Promise<any> | any | void
315
- (req: Request): Promise<any> | any | void
316
- }
317
-
318
- interface OnEventHandler {
319
- (req: Request, next: Function): Promise<any> | any | void
320
- }
321
-
322
- interface OnErrorHandler {
323
- (err: Error, req: Request): any | void
324
- }
325
-
326
- // `Partial` wraps any type and allows all properties to be undefined
327
- // in addition to their actual type.
328
- // This is important in the context of CRUD events, where
329
- // entities could be lacking any properties, like a non-existing ID
330
- // or when DB fields are corrupted, etc.
331
- type Partial<T> = { [Key in keyof T]: undefined | T[Key] }
332
-
333
- // Naming the first parameter in a handler `each` has special voodoo
334
- // semantic which makes it impossible for us to infer the exact behaviour on a type level.
335
- // So we always have to expect scalars as well as arrays in some callbacks.
336
- type OneOrMany<T> = T | T[]
337
-
338
- // functions generated by cds-typer explicitly carry types for
339
- // parameters and return type, as their names are not accessible from
340
- // function signatures to the type system.
341
- // This meta information is required in .on action handlers.
342
- type CdsFunction = {
343
- (...args: any[]): any,
344
- __parameters: object,
345
- __returns: any,
346
- }
347
-
348
- type TypedRequest<T> = Omit<Request, 'data'> & { data: T }
349
-
350
- // https://cap.cloud.sap/docs/node.js/core-services#srv-on-before-after
351
- declare namespace CRUDEventHandler {
352
- type Before<P, R> = (req: TypedRequest<P>) => Promise<R> | R
353
- type On<P, R> = (req: TypedRequest<P>, next: (...args: any[]) => Promise<R> | R) => Promise<R> | R
354
- type After<P, R> = (data: undefined | P, req: TypedRequest<P>) => Promise<R> | R
355
- }
356
-
357
- // Handlers for actions try to infer the passed .data property
358
- // as strictly as possible and therefore have to remove
359
- // { data: any } (inherited EventMessage} with a more restricted
360
- // type, based on the parameters of the action.
361
- interface ActionEventHandler<P, R> {
362
- (req: Omit<Request, 'data'> & { data: P }, next: Function): Promise<R> | R
363
- }
364
-
365
- // Note: the behaviour of ResultsHandler changes based on the name of the parameter.
366
- // If the parameter in the hook is called "each", it is called once for each row in the result,
367
- // otherwise it gets called exactly one time with the entire result.
368
- // This runtime behaviour can not be described on type level
369
- // (in a way that would benefit the user).
370
- // The user will therefore receive "any" as their result/ each. If we could some day differentiate,
371
- // we may want to add a generic to ResultsHandler which is passed from the EventHandlers down below.
372
- interface ResultsHandler {
373
- (results: any[], req: Request): void
374
- (each: any, req: Request): void
375
- }
376
-
377
- interface SpawnEvents {
378
- succeeded: (res: any) => void
379
- failed: (error: any) => void
380
- done: () => void
381
- }
382
-
383
- declare class SpawnEventEmitter {
384
-
385
- on<U extends keyof SpawnEvents>(
386
- event: U, listener: SpawnEvents[U]
387
- ): this
388
-
389
- emit<U extends keyof SpawnEvents>(
390
- event: U, ...args: Parameters<SpawnEvents[U]>
391
- ): boolean
392
- timer: any
393
-
394
- }
395
-
396
- declare namespace types {
397
- type event = eventName | eventName[]
398
- type eventName = string
399
- | 'CREATE' | 'READ' | 'UPDATE' | 'DELETE'
400
- | 'NEW' | 'EDIT' | 'PATCH' | 'SAVE'
401
- | 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE'
402
- | 'COMMIT' | 'ROLLBACK'
403
- type target = string | LinkedDefinition | LinkedEntity | (string | LinkedDefinition | LinkedEntity)[] | ArrayConstructable
404
- }
405
-
406
- type SpawnOptions = {
407
- [key: string]: any,
408
- every?: number,
409
- after?: number,
410
- }
411
-
412
- // FIXME: this was ?: EventContext before. Is context supposed to not be present sometimes?
413
- // let, as apparently we can reassign?
414
- /**
415
- * @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx#event-contexts
416
- */
417
- export let context: EventContext | undefined
418
-
419
- /**
420
- * @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx#cds-spawn)
421
- */
422
- export function spawn (options: SpawnOptions, fn: (tx: Transaction) => object): SpawnEventEmitter
423
-
424
-
425
- // facade proxies into cds.db, which is a Service
426
- /**
427
- * Starts or joins a transaction
428
- * @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx)
429
- */
430
- export const tx: {
431
- (fn: (tx: Transaction) => object): Promise<any>,
432
- (context?: object): Transaction,
433
- (context: object, fn: (tx: Transaction) => object): Promise<any>,
434
- }
435
- export const entities: Service['entities']
436
- export const run: Service['run']
437
- export const foreach: Service['foreach']
438
- export const stream: Service['stream']
439
- export const read: Service['read']
440
- export const create: Service['create']
441
- export const insert: Service['insert']
442
- export const update: Service['update']
443
- // temporarily moved to cds.d.ts, as "delete" is a reserved keyword
444
- // export const delete: Service['delete']
445
- // FIXME: see Service.disconnect comment
446
- // export const disconnect: Service['disconnect']
447
- export const transaction: Service['transaction']
448
- export const db: DatabaseService
449
- // export const upsert: Service['upsert']
package/apis/test.d.ts DELETED
@@ -1,112 +0,0 @@
1
- import { AxiosInstance } from 'axios'
2
- import chai from 'chai'
3
- import * as http from 'http'
4
- import { Service } from './services'
5
- import * as cds from './cds'
6
-
7
- type _cds = typeof cds
8
-
9
- declare class Axios {
10
-
11
- get axios (): AxiosInstance
12
- get: AxiosInstance['get'];
13
-
14
- put: AxiosInstance['put']
15
-
16
- post: AxiosInstance['post']
17
-
18
- patch: AxiosInstance['patch']
19
-
20
- delete: AxiosInstance['delete']
21
-
22
- options: AxiosInstance['options']
23
-
24
- get GET (): Axios['get']
25
-
26
- get PUT (): Axios['put']
27
-
28
- get POST (): Axios['post']
29
-
30
- get PATCH (): Axios['patch']
31
-
32
- get DELETE (): Axios['delete']
33
-
34
- get OPTIONS (): Axios['options']
35
-
36
- }
37
-
38
- declare class DataUtil {
39
-
40
- delete (db?: Service): Promise<void>
41
-
42
- reset (db?: Service): Promise<void>
43
-
44
- /**
45
- * @deprecated if needed, call `reset()`, considering test performance
46
- */
47
- autoReset (enabled: boolean): this
48
-
49
- }
50
-
51
- declare class Test extends Axios {
52
-
53
- test: Test
54
-
55
- run (cmd: string, ...args: string[]): this
56
-
57
- in (...paths: string[]): this
58
-
59
- silent (): this
60
-
61
- /**
62
- * @deprecated Server log is shown by default. Use `log()` to get control over it.
63
- */
64
- verbose (v: boolean): this
65
-
66
- get chai (): typeof chai
67
-
68
- get expect (): typeof chai.expect
69
-
70
- get assert (): typeof chai.assert
71
-
72
- get data (): DataUtil
73
-
74
- get cds (): _cds
75
-
76
- log (): {
77
- output: string,
78
- clear(): void,
79
- release(): void,
80
- }
81
-
82
- then (r: (args: { server: http.Server, url: string }) => void): void
83
-
84
- // get sleep(): (ms: number) => Promise<void>;
85
- // get spy(): <T, K extends keyof T>(o: T, f: K) => T[K] extends (...args: infer TArgs) => infer TReturnValue
86
- // ? Spy<TArgs, TReturnValue>
87
- // : Spy;
88
-
89
- }
90
-
91
- // typings for spy inspired by @types/sinon
92
- // interface Spy<TArgs extends any[] = any[], TReturnValue = any> {
93
- // (...args: TArgs): TReturnValue;
94
- // called: number;
95
- // restore(): (...args: TArgs) => TReturnValue;
96
- // }
97
-
98
- declare const test: {
99
- Test: typeof Test,
100
-
101
- /**
102
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/cds-test#class-cds-test-test)
103
- */
104
- (dirname: string): Test,
105
-
106
- /**
107
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/cds-test#class-cds-test-test)
108
- */
109
- (command: string, ...args: string[]): Test,
110
-
111
- in (dirname: string): Test,
112
- }
package/apis/utils.d.ts DELETED
@@ -1,90 +0,0 @@
1
- import * as fs from 'node:fs'
2
-
3
- /**
4
- * Provides a set of utility functionss
5
- */
6
- declare const utils: {
7
-
8
- /**
9
- * Generates a new v4 UUID
10
- * @see https://cap.cloud.sap/docs/node.js/cds-facade#cds-utils
11
- */
12
- uuid (): string,
13
-
14
- /**
15
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#decodeuri
16
- */
17
- decodeURI(input: string): string,
18
-
19
- /**
20
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#decodeuricomponent
21
- */
22
- decodeURIComponent(input: string): string,
23
-
24
- /**
25
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#local-filename
26
- */
27
- local(filename: string): string,
28
-
29
- /**
30
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#exists-file
31
- */
32
- exist(file: string): boolean,
33
-
34
- /**
35
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#isdir-file
36
- */
37
- isdir(file: string): string,
38
-
39
- /**
40
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#isdir-file
41
- */
42
- isfile(file: string): string,
43
-
44
- /**
45
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-read-file
46
- */
47
- read(file: string): Promise<Buffer | object>,
48
-
49
- /**
50
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-write-data-to-file
51
- */
52
- write: {
53
- (data: object): {
54
- to(...file: string[]): Promise<ReturnType<typeof fs.promises.writeFile>>,
55
- },
56
- (file: string, data: object): Promise<ReturnType<typeof fs.promises.writeFile>>,
57
- },
58
-
59
- /**
60
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-copy-src-to-dst
61
- */
62
- copy: {
63
- (src: string): {
64
- to(...dst: string[]): Promise<ReturnType<typeof fs.promises.copyFile>>,
65
- },
66
- (dst: string, src: string): Promise<ReturnType<typeof fs.promises.copyFile>>,
67
- },
68
-
69
- /**
70
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-mkdirp-path
71
- */
72
- mkdirp: (...path: string[]) => Promise<string>,
73
-
74
- /**
75
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-rmdir-path
76
- */
77
- rmdir: (...path: string[]) => Promise<ReturnType<typeof fs.promises.rm>>,
78
-
79
-
80
- /**
81
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-rimraf-path
82
- */
83
- rimraf: (...path: string[]) => Promise<ReturnType<typeof fs.promises.rm>>,
84
-
85
-
86
- /**
87
- * @see https://cap.cloud.sap/docs/node.js/cds-utils#async-rm-path
88
- */
89
- rm: (...path: string[]) => Promise<ReturnType<typeof fs.promises.rm>>,
90
- }