@cap-js/cds-types 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # cds-types
2
2
 
3
- <!-- [![REUSE status](https://api.reuse.software/badge/github.com/cap-js/cds-types)](https://api.reuse.software/info/github.com/cap-js/cds-types)
4
- ![Unit Tests passing](https://github.com/cap-js/cds-types/actions/workflows/test.yml/badge.svg) -->
3
+ [![REUSE status](https://api.reuse.software/badge/github.com/cap-js/cds-types)](https://api.reuse.software/info/github.com/cap-js/cds-types)
4
+ ![Unit Tests passing](https://github.com/cap-js/cds-types/actions/workflows/test.yml/badge.svg)
5
5
 
6
6
  ## About this project
7
7
 
package/apis/cds.d.ts CHANGED
@@ -1,33 +1,37 @@
1
- import * as ql from './ql'
1
+ export * from './core'
2
+ export * from './server'
3
+ export * from './env'
4
+ export * from './models'
5
+ export * from './services'
6
+ export * from './events'
7
+ export * from './utils'
8
+ export { log, debug } from './log'
9
+ export { test } from './test'
10
+ export * from './cqn'
2
11
 
3
- declare global {
4
- const cds : cds_facade
5
- }
12
+ // FIXME: sort out what needs to be exported from csn/linked and under which namespace
13
+ // export { Association, CSN, Definition, Extension, Element, EntityElements, FQN, kinds } from './csn'
14
+ // export { Definitions, LinkedCSN, LinkedDefinition, LinkedAssociation, LinkedEntity, Filter, Visitor } from './linked'
6
15
 
7
- export = cds
16
+ // API extractor cannot handle export * as ql from './ql', so split it into an import and an export statement
17
+ import * as ql from './ql'
18
+ export { ql }
19
+ export { QLExtensions } from './ql' // cds-ql.ts test tries to import this from top level? Correct? Or ql.QLExtensions?
8
20
 
9
- type cds_facade = {}
10
- & import('./core').default
11
- & import('./env').default
12
- & import('./models').default
13
- & import('./server').default
14
- & import('./services').QueryAPI
15
- & import('./services').default
16
- & import('./events').default
17
- & import('./ql').cds_ql
18
- & import('./log')
19
- & import('./utils')
20
- & import('./test')
21
+ // trick to work around "delete" as reserved identifier
22
+ import { Service } from './services'
23
+ declare const delete_: Service['delete']
24
+ export { delete_ as delete }
21
25
 
22
26
  declare global {
23
27
  // these provide the functionality from SELECT, INSERT, etc in the global facade
24
- const SELECT: typeof cds.ql.SELECT
25
- const INSERT: typeof cds.ql.INSERT
26
- const UPSERT: typeof cds.ql.UPSERT
27
- const UPDATE: typeof cds.ql.UPDATE
28
- const DELETE: typeof cds.ql.DELETE
29
- const CREATE: typeof cds.ql.CREATE
30
- const DROP: typeof cds.ql.DROP
28
+ const SELECT: ql.QL<any>['SELECT']
29
+ const INSERT: ql.QL<any>['INSERT']
30
+ const UPSERT: ql.QL<any>['UPSERT']
31
+ const UPDATE: ql.QL<any>['UPDATE']
32
+ const DELETE: ql.QL<any>['DELETE']
33
+ const CREATE: ql.QL<any>['CREATE']
34
+ const DROP: ql.QL<any>['DROP']
31
35
 
32
36
  // and these allow us to use them as type too, i.e. `const q: SELECT<Book> = ...`
33
37
  type SELECT<T> = ql.SELECT<T>
@@ -37,4 +41,4 @@ declare global {
37
41
  type DELETE<T> = ql.DELETE<T>
38
42
  type CREATE<T> = ql.CREATE<T>
39
43
  type DROP<T> = ql.DROP<T>
40
- }
44
+ }
package/apis/core.d.ts CHANGED
@@ -2,100 +2,94 @@ import { LinkedAssociation, LinkedEntity, linked } from './linked'
2
2
  import * as csn from './csn'
3
3
  import { service } from './server'
4
4
 
5
+ type Intersect<T extends readonly unknown[]> = T extends [infer Head, ...infer Tail]
6
+ ? Head & Intersect<Tail>
7
+ : unknown
8
+
5
9
  // These are classes actually -> using the new() => interface trick
10
+ /**
11
+ * Base class for linked Associations from reflected models.
12
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
13
+ */
6
14
  export type Association = new(_?:object) => LinkedAssociation
15
+ /**
16
+ * Base class for linked Compositions from reflected models.
17
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
18
+ */
7
19
  export type Composition = new(_?:object) => LinkedAssociation
20
+ /**
21
+ * Base class for linked entities from reflected models.
22
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-entity)
23
+ */
8
24
  export type entity = new(_?:object) => LinkedEntity
9
25
  export type event = new(_?:object) => linked & csn.struct
10
26
  export type type = new(_?:object) => linked & csn.type
11
27
  export type array = new(_?:object) => linked & csn.type
12
28
  export type struct = new(_?:object) => linked & csn.struct
13
29
 
14
- export default class cds {
15
- // infer (query : cqn, model : csn) : LinkedDefinition
16
-
17
- builtin: {
18
- /**
19
- * Base classes of linked definitions from reflected models.
20
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-builtin-classes)
21
- */
22
- classes: {
23
- Association: Association
24
- Composition: Composition
25
- entity: entity
26
- event: event
27
- type: type
28
- array: array
29
- struct: struct
30
- service: service
31
- }
32
- types: {}
33
- }
34
-
35
- /**
36
- * Base class for linked Associations from reflected models.
37
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
38
- */
39
- Association: Association
40
-
41
- /**
42
- * Base class for linked Compositions from reflected models.
43
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
44
- */
45
- Composition: Composition
46
-
47
- /**
48
- * Base class for linked entities from reflected models.
49
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-entity)
50
- */
51
- entity: entity
52
-
53
- event: event
54
- type: type
55
- array: array
56
- struct: struct
57
-
30
+ // infer (query : cqn, model : csn) : LinkedDefinition
31
+ export const builtin: {
58
32
  /**
59
- * Add aspects to a given object, for example:
60
- *
61
- * extend (Object.prototype) .with (class {
62
- * get foo() { return ... }
63
- * bar() {...}
64
- * }.prototype)
33
+ * Base classes of linked definitions from reflected models.
34
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-builtin-classes)
65
35
  */
66
- extend<T>(target: T): {
67
- with<E extends readonly unknown[]>(...ext: E): T & Intersect<E>
36
+ classes: {
37
+ Association: Association
38
+ Composition: Composition
39
+ entity: entity
40
+ event: event
41
+ type: type
42
+ array: array
43
+ struct: struct
44
+ service: service
68
45
  }
46
+ types: {}
47
+ }
69
48
 
70
- /**
71
- * Equip a given facade object with getters for lazy-loading modules instead
72
- * of static requires. Example:
73
- *
74
- * const facade = lazify ({
75
- * sub: lazy => require ('./sub-module')
76
- * })
77
- *
78
- * The first usage of `facade.sub` will load the sub module
79
- * using standard Node.js's `module.require` functions.
80
- */
81
- lazify <T>(target: T) : T
82
-
83
- /**
84
- * Prepare a node module for lazy-loading submodules instead
85
- * of static requires. Example:
86
- *
87
- * require = lazify (module) //> turns require into a lazy one
88
- * const facade = module.exports = {
89
- * sub: require ('./sub-module')
90
- * })
91
- *
92
- * The first usage of `facade.sub` will load the sub module
93
- * using standard Node.js's `module.require` functions.
94
- */
95
- lazified <T>(target: T) : T
96
-
49
+ /**
50
+ * Add aspects to a given object, for example:
51
+ *
52
+ * @example
53
+ * ```js
54
+ * extend (Object.prototype) .with (class {
55
+ * get foo() { return ... }
56
+ * bar() {...}
57
+ * }.prototype)
58
+ * ```
59
+ */
60
+ export function extend<T>(target: T): {
61
+ with<E extends readonly unknown[]>(...ext: E): T & Intersect<E>
97
62
  }
98
63
 
99
- type Intersect<T extends readonly unknown[]> = T extends [infer Head, ...infer Tail]
100
- ? Head & Intersect<Tail>
101
- : unknown
64
+ /**
65
+ * Equip a given facade object with getters for lazy-loading modules instead
66
+ * of static requires. Example:
67
+ *
68
+ * @example
69
+ * ```js
70
+ * const facade = lazify ({
71
+ * sub: lazy => require ('./sub-module')
72
+ * })
73
+ * ```
74
+ *
75
+ * The first usage of `facade.sub` will load the sub module
76
+ * using standard Node.js's `module.require` functions.
77
+ */
78
+ export function lazify <T>(target: T) : T
79
+
80
+ /**
81
+ * Prepare a node module for lazy-loading submodules instead
82
+ * of static requires. Example:
83
+ *
84
+ * @example
85
+ * ```js
86
+ * require = lazify (module) //> turns require into a lazy one
87
+ * const facade = module.exports = {
88
+ * sub: require ('./sub-module')
89
+ * })
90
+ * ```
91
+ *
92
+ * The first usage of `facade.sub` will load the sub module
93
+ * using standard Node.js's `module.require` functions.
94
+ */
95
+ export function lazified <T>(target: T) : T
package/apis/cqn.d.ts CHANGED
@@ -56,20 +56,31 @@ export type DROP = {DROP:{
56
56
  view: ref
57
57
  }}
58
58
 
59
+ /** @private */
59
60
  type scalar = number | string | boolean | null
61
+ /** @private */
60
62
  type data = Record<string,any>
63
+ /** @private */
61
64
  type name = string
65
+ /** @private */
62
66
  type source = ( ref | SELECT ) & { as?: name, join?:name, on?:xpr }
63
67
  export type column_expr = expr & { as?: name, cast?:any, expand?: column_expr[], inline?: column_expr[] }
64
68
  export type predicate = _xpr
69
+ /** @private */
65
70
  type ordering_term = expr & { sort?: "asc"|"desc", nulls?: "first"|"last" }
66
71
 
67
72
  export type expr = ref | val | xpr | function_call | SELECT
73
+ /** @private */
68
74
  type ref = {ref:( name & { id?:string, where?:expr, args?:expr[] } )[]}
75
+ /** @private */
69
76
  type val = {val:any}
77
+ /** @private */
70
78
  type xpr = {xpr:_xpr}
79
+ /** @private */
71
80
  type _xpr = ( expr | operator ) []
81
+ /** @private */
72
82
  type operator = string
83
+ /** @private */
73
84
  type function_call = {func: string, args: {[key: string]: unknown}[]}
74
85
 
75
86
  export type enum_literal = {"#": string}
package/apis/csn.d.ts CHANGED
@@ -46,7 +46,7 @@ export type Definition = context & service & type & struct & entity & Associatio
46
46
  // NOTE: If we use & instead of | CSN.definitions values would be reduced to <never>
47
47
 
48
48
  /**
49
- * Extensions capture extend Foo with { ... } directives.
49
+ * Extensions capture `extend Foo with { ... }` directives.
50
50
  */
51
51
  export type Extension = {
52
52
  extend: FQN
@@ -103,7 +103,7 @@ export interface Association extends type {
103
103
  type: 'cds.Association' | 'cds.Composition'
104
104
  target: FQN
105
105
  /**
106
- * The specified cardinality. to-one = {max:1}, to-many = {max:'*'}
106
+ * The specified cardinality. to-one = `{max:1}`, to-many = `{max:'*'}`
107
107
  */
108
108
  cardinality?: { src?: 1; min?: 1 | 0; max?: 1 | '*' }
109
109
  /**
package/apis/env.d.ts CHANGED
@@ -1,11 +1,10 @@
1
- export default class {
2
- /**
3
- * Access to the configuration for Node.js runtime and tools.
4
- * The object is the effective result of configuration merged from various sources,
5
- * filtered through the currently active profiles, thus highly dependent on the current working
6
- * directory and process environment.
7
- */
8
- env : {
1
+ /**
2
+ * Access to the configuration for Node.js runtime and tools.
3
+ * The object is the effective result of configuration merged from various sources,
4
+ * filtered through the currently active profiles, thus highly dependent on the current working
5
+ * directory and process environment.
6
+ */
7
+ export const env : {
9
8
  build: any,
10
9
  hana: any,
11
10
  i18n: any,
@@ -15,11 +14,9 @@ export default class {
15
14
  odata: any,
16
15
  query: any,
17
16
  sql: any
18
- }
17
+ } & { [key: string]: any } // to allow additional values we have not yet captured
19
18
 
20
- requires: any
21
- version: string
22
- home: string
23
- root: string
24
-
25
- }
19
+ export const requires: any
20
+ export const version: string
21
+ export const home: string
22
+ export const root: string
package/apis/events.d.ts CHANGED
@@ -1,32 +1,7 @@
1
1
  import { LinkedDefinition } from './linked'
2
2
  import { Query } from './cqn'
3
3
  import { ref } from './cqn'
4
- import * as express from "express"
5
-
6
-
7
- export default class cds {
8
-
9
- /**
10
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
11
- */
12
- EventContext: typeof EventContext
13
-
14
- /**
15
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
16
- */
17
- Event: typeof Event
18
-
19
- /**
20
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
21
- */
22
- Request: typeof Request
23
-
24
- /**
25
- * Represents the user in a given context.
26
- * @see [capire docs](https://cap.cloud.sap/docs/node.js/authentication#cds-user)
27
- */
28
- User: typeof User
29
- }
4
+ import * as express from 'express'
30
5
 
31
6
 
32
7
  /**
@@ -34,7 +9,7 @@ export default class cds {
34
9
  * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
35
10
  */
36
11
  export class EventContext {
37
- constructor(properties:{event:string, data?:object, query?:object, headers:object});
12
+ constructor(properties:{event:string, data?:object, query?:object, headers?:object});
38
13
  http?: {req: express.Request, res: express.Response}
39
14
  tenant: string
40
15
  user: User
@@ -62,7 +37,7 @@ export class Request extends Event {
62
37
  path: string
63
38
  target: LinkedDefinition
64
39
  /**
65
- * Shortcut to {@link target.name}
40
+ * Shortcut to {@link Request.target | target (entity) name}
66
41
  * @see https://cap.cloud.sap/docs/node.js/events#req-entity
67
42
  */
68
43
  entity: string
@@ -97,6 +72,10 @@ export class Request extends Event {
97
72
  }
98
73
 
99
74
 
75
+ /**
76
+ * Represents the user in a given context.
77
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/authentication#cds-user)
78
+ */
100
79
  export class User {
101
80
  constructor(obj?: string | { id: string; attr: Record<string, string>; roles: Record<string, string> } | User)
102
81
  id: string
package/apis/linked.d.ts CHANGED
@@ -2,7 +2,9 @@ import { CSN, FQN, Association, Definition, entity, kinds } from "./csn"
2
2
 
3
3
  export type LinkedDefinition = linked & Definition & LinkedEntity & LinkedAssociation
4
4
  export type Definitions = { [name: string]: LinkedDefinition }
5
-
5
+ // FIXME: this is only a temporary alias. Definitions is actually correct,
6
+ // but the name may be misleading, as it is indeed a mapping of strings to LinkedDefinition objects.
7
+ export type LinkedDefinitions = Definitions
6
8
  export interface linked {
7
9
  is(kind: kinds | 'Association' | 'Composition'): boolean
8
10
  name: FQN
@@ -24,10 +26,12 @@ export interface LinkedCSN extends CSN {
24
26
  /**
25
27
  * Fetches definitions matching the given filter, returning an iterator on them.
26
28
  * @example
27
- * let m = cds.reflect (aParsedModel)
28
- * for (let d of m.each('entity')) console.log (d.kind, d.name)
29
- * let entities = [...m.each('entity')] //> capture all
30
- * let entities = m.all('entity') //> equivalent shortcut
29
+ * ```js
30
+ * let m = cds.reflect (aParsedModel)
31
+ * for (let d of m.each('entity')) console.log (d.kind, d.name)
32
+ * let entities = [...m.each('entity')] //> capture all
33
+ * let entities = m.all('entity') //> equivalent shortcut
34
+ * ```
31
35
  */
32
36
  each(x: Filter, defs?: Definitions): IterableIterator<any>
33
37
 
@@ -41,8 +45,8 @@ export interface LinkedCSN extends CSN {
41
45
  * Fetches definitions matching the given filter, returning the first match, if any.
42
46
  * @example
43
47
  * let service = model.find('service')
44
- * @param {Filter} [x] the filter
45
- * @param {Definitions} [defs] the definitions to fetch in, default: `this.definitions`
48
+ * @param x - the filter
49
+ * @param defs - the definitions to fetch in, default: `this.definitions`
46
50
  */
47
51
  find(x: Filter, defs?: Definitions): any
48
52
 
@@ -65,10 +69,12 @@ export interface LinkedCSN extends CSN {
65
69
  * It fetches all definitions whose fully-qualified names start with the parent's name.
66
70
  * Returns the found definitions as an object with the local names as keys.
67
71
  * @example
68
- * let service = model.find ('service')
69
- * let entities = m.childrenOf (service)
70
- * @param parent either the parent itself or its fully-qualified name
71
- * @param filter an optional filter to apply before picking a child
72
+ * ```js
73
+ * let service = model.find ('service')
74
+ * let entities = m.childrenOf (service)
75
+ * ```
76
+ * @param parent - either the parent itself or its fully-qualified name
77
+ * @param filter - an optional filter to apply before picking a child
72
78
  */
73
79
  childrenOf(parent: any | string, filter?: ((def: LinkedDefinition) => boolean)): Definitions
74
80
 
@@ -78,6 +84,7 @@ export interface LinkedCSN extends CSN {
78
84
  * working with fully-qualified names as follows:
79
85
  *
80
86
  * @example
87
+ * ```js
81
88
  * let model = cds.reflect (cds.parse(`
82
89
  * namespace our.lovely.bookshop;
83
90
  * entity Books {...}
@@ -85,6 +92,7 @@ export interface LinkedCSN extends CSN {
85
92
  * `))
86
93
  * const {Books,Authors} = model.exports
87
94
  * SELECT.from (Books) .where ({ID:11})
95
+ * ```
88
96
  */
89
97
  exports: Definitions & ((namespace: string) => Definitions)
90
98
  entities: Definitions & ((namespace: string) => Definitions)
package/apis/log.d.ts CHANGED
@@ -1,23 +1,22 @@
1
- export = cds
2
- declare class cds {
3
- /**
4
- * Create a new logger, or install a custom log formatter
5
- */
6
- log: LogFactory
7
1
 
8
- /**
9
- * Shortcut to `cds.log(...).debug`, returning `undefined` if `cds.log(...)._debug` is `false`.
10
- * Use like this:
11
- * ```
12
- * const dbg = cds.debug('foo')
13
- * ...
14
- * dbg && dbg('message')
15
- * ```
16
- *
17
- * @param name logger name
18
- */
19
- debug(name: string): undefined | Log
20
- }
2
+ /**
3
+ * Create a new logger, or install a custom log formatter
4
+ */
5
+ export declare const log: LogFactory
6
+
7
+ /**
8
+ * Shortcut to `cds.log(...).debug`, returning `undefined` if `cds.log(...)._debug` is `false`.
9
+ * Use like this:
10
+ * @example
11
+ * ```js
12
+ * const dbg = cds.debug('foo')
13
+ * ...
14
+ * dbg && dbg('message')
15
+ * ```
16
+ *
17
+ * @param name - logger name
18
+ */
19
+ export declare function debug(name: string): undefined | Log
21
20
 
22
21
  declare type LogFactory = {
23
22
 
@@ -28,23 +27,23 @@ declare type LogFactory = {
28
27
  *
29
28
  * By default this logger would prefix all output with `[sql] - `
30
29
  * You can change this by specifying another prefix in the options:
31
- *
32
- * ```
30
+ * @example
31
+ * ```js
33
32
  * const LOG = cds.log('sql|db', { prefix: 'cds.ql' })
34
33
  * ```
35
34
  *
36
35
  * Call `cds.log()` for a given module again to dynamically change the log level
37
36
  * of all formerly created loggers, for example:
38
- *
39
- * ```
37
+ * @example
38
+ * ```js
40
39
  * const LOG = cds.log('sql')
41
40
  * LOG.info ('this will show, as default level is info')
42
41
  * cds.log('sql', 'warn')
43
42
  * LOG.info('this will be suppressed now')
44
43
  * ```
45
44
  *
46
- * @param name logger name
47
- * @param options level, label and prefix
45
+ * @param name - logger name
46
+ * @param options - level, label and prefix
48
47
  * @returns the logger
49
48
  * @see [capire](https://cap.cloud.sap/docs/node.js/cds-log)
50
49
  */
@@ -52,17 +51,17 @@ declare type LogFactory = {
52
51
 
53
52
  /**
54
53
  * Set a custom formatter function like that:
55
- * ```
54
+ * ```js
56
55
  * cds.log.format = (module, level, ...args) => [ '[', module, ']', ...args ]
57
56
  * ```
58
57
  *
59
58
  * The formatter shall return an array of arguments, which are passed to the logger (for example, `console.log()`)
60
59
  */
61
60
  format: Formatter
62
-
61
+
63
62
  /**
64
63
  * Set a custom logger.
65
- * ```
64
+ * ```js
66
65
  * cds.log.Logger = ...
67
66
  * ```
68
67
  */
@@ -142,9 +141,9 @@ declare type Formatter = {
142
141
  /**
143
142
  * Custom format function
144
143
  *
145
- * @param module logger name
146
- * @param level log level
147
- * @param args additional arguments
144
+ * @param module - logger name
145
+ * @param level - log level
146
+ * @param args - additional arguments
148
147
  * @returns an array of arguments, which are passed to the logger (for example, `console.log()`)
149
148
  */
150
149
  (module: string, level: number, args: any[]): any[]
@@ -154,8 +153,8 @@ declare type Log = {
154
153
  /**
155
154
  * Logs a message
156
155
  *
157
- * @param message text to log
158
- * @param optionalParams additional parameters, same as in `console.log(text, param1, ...)`
156
+ * @param message - text to log
157
+ * @param optionalParams - additional parameters, same as in `console.log(text, param1, ...)`
159
158
  */
160
159
  (message?: any, ...optionalParams: any[]): void
161
160
  }