@cap-js/cds-types 0.0.1 → 0.2.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
 
@@ -15,8 +15,19 @@ Find more information on the APIs in the [Node.js SDK documentation](https://cap
15
15
 
16
16
  ## Support, Feedback, Contributing
17
17
 
18
+ ### Local Setup
19
+
20
+ After cloning, just run
21
+
22
+ ```sh
23
+ npm run setup
24
+ ```
25
+
26
+ which installs all dependencies.
27
+
18
28
  This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/cds-types/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
19
29
 
30
+
20
31
  ## Security / Disclosure
21
32
 
22
33
  If you find any bug that may be a security problem, please follow our instructions at [in our security policy](https://github.com/SAP/.github/blob/main/SECURITY.md) on how to report it. Please do not create GitHub issues for security-related doubts or problems.
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
- // 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
27
+ // these provide the functionality from SELECT, INSERT, etc in the global facade
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>
@@ -36,5 +40,5 @@ declare global {
36
40
  type UPDATE<T> = ql.UPDATE<T>
37
41
  type DELETE<T> = ql.DELETE<T>
38
42
  type CREATE<T> = ql.CREATE<T>
39
- type DROP<T> = ql.DROP<T>
43
+ type DROP<T> = ql.DROP<T>
40
44
  }
package/apis/core.d.ts CHANGED
@@ -2,100 +2,107 @@ import { LinkedAssociation, LinkedEntity, linked } from './linked'
2
2
  import * as csn from './csn'
3
3
  import { service } from './server'
4
4
 
5
- // These are classes actually -> using the new() => interface trick
6
- export type Association = new(_?:object) => LinkedAssociation
7
- export type Composition = new(_?:object) => LinkedAssociation
8
- export type entity = new(_?:object) => LinkedEntity
9
- export type event = new(_?:object) => linked & csn.struct
10
- export type type = new(_?:object) => linked & csn.type
11
- export type array = new(_?:object) => linked & csn.type
12
- export type struct = new(_?:object) => linked & csn.struct
5
+ type Intersect<T extends readonly unknown[]> = T extends [infer Head, ...infer Tail]
6
+ ? Head & Intersect<Tail>
7
+ : unknown
13
8
 
14
- export default class cds {
15
- // infer (query : cqn, model : csn) : LinkedDefinition
9
+ /**
10
+ * Base class for linked Associations from reflected models.
11
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
12
+ */
13
+ export interface Association extends LinkedAssociation {}
14
+ export declare class Association { constructor (_?: object) }
16
15
 
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
- }
16
+ /**
17
+ * Base class for linked Compositions from reflected models.
18
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-composition)
19
+ */
20
+ export interface Composition extends Association {}
21
+ export declare class Composition { constructor (_?: object) }
34
22
 
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
23
+ /**
24
+ * Base class for linked entities from reflected models.
25
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-entity)
26
+ */
27
+ export interface entity extends LinkedEntity {}
28
+ export declare class entity { constructor (_?: object) }
40
29
 
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
30
+ export interface event extends linked, csn.struct {}
31
+ export class event { constructor (_?: object) }
46
32
 
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
33
+ export interface type extends linked, csn.type {}
34
+ export class type { constructor (_?: object) }
52
35
 
53
- event: event
54
- type: type
55
- array: array
56
- struct: struct
36
+ export interface array extends linked, csn.type {}
37
+ export class array { constructor (_?: object) }
57
38
 
58
- /**
59
- * Add aspects to a given object, for example:
60
- *
61
- * extend (Object.prototype) .with (class {
62
- * get foo() { return ... }
63
- * bar() {...}
64
- * }.prototype)
65
- */
66
- extend<T>(target: T): {
67
- with<E extends readonly unknown[]>(...ext: E): T & Intersect<E>
68
- }
39
+ export interface struct extends linked, csn.struct {}
40
+ export class struct { constructor (_?: object) }
69
41
 
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
42
+ // infer (query : cqn, model : csn) : LinkedDefinition
43
+ export const builtin: {
82
44
 
83
45
  /**
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.
46
+ * Base classes of linked definitions from reflected models.
47
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-builtin-classes)
94
48
  */
95
- lazified <T>(target: T) : T
49
+ classes: {
50
+ Association: typeof Association,
51
+ Composition: typeof Composition,
52
+ entity: typeof entity,
53
+ event: typeof event,
54
+ type: typeof type,
55
+ array: typeof array,
56
+ struct: typeof struct,
57
+ service: service,
58
+ },
59
+ types: Record<string, object>,
60
+ }
96
61
 
62
+ /**
63
+ * Add aspects to a given object, for example:
64
+ *
65
+ * @example
66
+ * ```js
67
+ * extend (Object.prototype) .with (class {
68
+ * get foo() { return ... }
69
+ * bar() {...}
70
+ * }.prototype)
71
+ * ```
72
+ */
73
+ export function extend<T> (target: T): {
74
+ with<E extends readonly any[]>(...ext: E): T & Intersect<E>,
97
75
  }
98
76
 
99
- type Intersect<T extends readonly unknown[]> = T extends [infer Head, ...infer Tail]
100
- ? Head & Intersect<Tail>
101
- : unknown
77
+ /**
78
+ * Equip a given facade object with getters for lazy-loading modules instead
79
+ * of static requires. Example:
80
+ *
81
+ * @example
82
+ * ```js
83
+ * const facade = lazify ({
84
+ * sub: lazy => require ('./sub-module')
85
+ * })
86
+ * ```
87
+ *
88
+ * The first usage of `facade.sub` will load the sub module
89
+ * using standard Node.js's `module.require` functions.
90
+ */
91
+ export function lazify<T> (target: T): T
92
+
93
+ /**
94
+ * Prepare a node module for lazy-loading submodules instead
95
+ * of static requires. Example:
96
+ *
97
+ * @example
98
+ * ```js
99
+ * require = lazify (module) //> turns require into a lazy one
100
+ * const facade = module.exports = {
101
+ * sub: require ('./sub-module')
102
+ * })
103
+ * ```
104
+ *
105
+ * The first usage of `facade.sub` will load the sub module
106
+ * using standard Node.js's `module.require` functions.
107
+ */
108
+ export function lazified<T> (target: T): T
package/apis/cqn.d.ts CHANGED
@@ -1,76 +1,98 @@
1
- import { entity } from "./csn" // cyclic dependency
1
+ import { entity } from './csn' // cyclic dependency
2
+ import { UnionToIntersection, UnionsToIntersections } from './internal/inference'
2
3
 
3
4
  // FIXME: a union type would be more appropriate here
4
5
  export type Query = Partial<SELECT & INSERT & UPDATE & DELETE & CREATE & DROP & UPSERT>
5
6
 
6
- export type SELECT = {SELECT:{
7
- distinct?: true
8
- one? : boolean
9
- from : source
10
- mixin?: {[key:string]: expr}
11
- columns? : column_expr[]
12
- excluding? : string[]
13
- where? : predicate
14
- having? : predicate
15
- groupBy? : expr[]
16
- orderBy? : ordering_term[]
17
- limit?: { rows:val, offset:val }
18
- }}
19
-
20
- export type INSERT = {INSERT:{
21
- into : ref | name
22
- entries : data[]
23
- columns : string[]
24
- values : scalar[]
25
- rows : scalar[][]
26
- as : SELECT
27
- }}
28
-
29
- export type UPSERT = {UPSERT:{
30
- into : ref | name
31
- columns : string[]
32
- entries : data[]
33
- values : scalar[]
34
- rows : scalar[][]
35
- }}
36
-
37
- export type UPDATE = {UPDATE:{
38
- entity : ref | name
39
- data : { [key:string] : expr }
40
- where? : predicate
41
- }}
42
-
43
- export type DELETE = {DELETE:{
44
- from : ref | name
45
- where? : predicate
46
- }}
47
-
48
- export type CREATE = {CREATE:{
49
- entity : entity | name
50
- as: SELECT
51
- }}
52
-
53
- export type DROP = {DROP:{
54
- entity : name
55
- table: ref
56
- view: ref
57
- }}
7
+ export type SELECT = { SELECT: {
8
+ distinct?: true,
9
+ one?: boolean,
10
+ from: source,
11
+ mixin?: { [key: string]: expr },
12
+ columns?: column_expr[],
13
+ excluding?: string[],
14
+ where?: predicate,
15
+ having?: predicate,
16
+ groupBy?: expr[],
17
+ orderBy?: ordering_term[],
18
+ limit?: { rows: val, offset: val },
19
+ }, }
58
20
 
21
+ export type INSERT = { INSERT: {
22
+ into: ref | name,
23
+ entries: data[],
24
+ columns: string[],
25
+ values: scalar[],
26
+ rows: scalar[][],
27
+ as: SELECT,
28
+ }, }
29
+
30
+ export type UPSERT = { UPSERT: {
31
+ into: ref | name,
32
+ columns: string[],
33
+ entries: data[],
34
+ values: scalar[],
35
+ rows: scalar[][],
36
+ }, }
37
+
38
+ export type UPDATE = { UPDATE: {
39
+ entity: ref | name,
40
+ data: { [key: string]: expr },
41
+ where?: predicate,
42
+ }, }
43
+
44
+ export type DELETE = { DELETE: {
45
+ from: ref | name,
46
+ where?: predicate,
47
+ }, }
48
+
49
+ export type CREATE = { CREATE: {
50
+ entity: entity | name,
51
+ as: SELECT,
52
+ }, }
53
+
54
+ export type DROP = { DROP: {
55
+ entity: name,
56
+ table: ref,
57
+ view: ref,
58
+ }, }
59
+
60
+ /** @private */
59
61
  type scalar = number | string | boolean | null
60
- type data = Record<string,any>
62
+
63
+ /** @private */
64
+ type data = Record<string, any>
65
+
66
+ /** @private */
61
67
  type name = string
62
- type source = ( ref | SELECT ) & { as?: name, join?:name, on?:xpr }
63
- export type column_expr = expr & { as?: name, cast?:any, expand?: column_expr[], inline?: column_expr[] }
64
- export type predicate = _xpr
65
- type ordering_term = expr & { sort?: "asc"|"desc", nulls?: "first"|"last" }
68
+
69
+ /** @private */
70
+ type source = UnionToIntersection<ref | SELECT> & { as?: name, join?: name, on?: xpr }
71
+ export type column_expr = UnionToIntersection<expr> & { as?: name, cast?: any, expand?: column_expr[], inline?: column_expr[] }
72
+ export type predicate = UnionsToIntersections<_xpr>
73
+
74
+ /** @private */
75
+ type ordering_term = expr & { sort?: 'asc' | 'desc', nulls?: 'first' | 'last' }
66
76
 
67
77
  export type expr = ref | val | xpr | function_call | SELECT
68
- type ref = {ref:( name & { id?:string, where?:expr, args?:expr[] } )[]}
69
- type val = {val:any}
70
- type xpr = {xpr:_xpr}
71
- type _xpr = ( expr | operator ) []
78
+
79
+ /** @private */
80
+ type ref = { ref: (name & { id?: string, where?: expr, args?: expr[] })[] }
81
+
82
+ /** @private */
83
+ type val = { val: any }
84
+
85
+ /** @private */
86
+ type xpr = { xpr: _xpr }
87
+
88
+ /** @private */
89
+ type _xpr = (expr | operator) []
90
+
91
+ /** @private */
72
92
  type operator = string
73
- type function_call = {func: string, args: {[key: string]: unknown}[]}
74
93
 
75
- export type enum_literal = {"#": string}
76
- export type expr_literal = {"=": string}
94
+ /** @private */
95
+ type function_call = { func: string, args: { [key: string]: any }[] }
96
+
97
+ export type enum_literal = { '#': string }
98
+ export type expr_literal = { '=': string }
package/apis/csn.d.ts CHANGED
@@ -4,6 +4,7 @@ import { SELECT, ref, predicate } from './cqn'
4
4
  * A parsed CDS model in CSN object notation.
5
5
  */
6
6
  export interface CSN {
7
+
7
8
  /**
8
9
  * The assigned namespace. If parsed from multiple sources,
9
10
  * this is the topmost model's namespace, if any, not the
@@ -46,19 +47,21 @@ export type Definition = context & service & type & struct & entity & Associatio
46
47
  // NOTE: If we use & instead of | CSN.definitions values would be reduced to <never>
47
48
 
48
49
  /**
49
- * Extensions capture extend Foo with { ... } directives.
50
+ * Extensions capture `extend Foo with { ... }` directives.
50
51
  */
51
52
  export type Extension = {
52
- extend: FQN
53
- elements?: { [name: string]: Element }
54
- includes?: FQN[]
53
+ extend: FQN,
54
+ elements?: { [name: string]: Element },
55
+ includes?: FQN[],
55
56
  }
56
57
 
57
58
  export type Element = type & struct & Association
58
59
 
59
60
  export type kinds = 'type' | 'entity' | 'event' | 'service' | 'context' | 'struct'
60
61
 
61
- export interface any_ { kind?: kinds }
62
+ export interface any_ {
63
+ kind?: kinds
64
+ }
62
65
  export interface context extends any_ { }
63
66
  export interface service extends any_ { }
64
67
 
@@ -68,6 +71,7 @@ export interface type extends any_ {
68
71
  }
69
72
 
70
73
  export interface struct extends type {
74
+
71
75
  /**
72
76
  * References to definitions to be included.
73
77
  * Not available after extensions have been applied.
@@ -76,40 +80,45 @@ export interface struct extends type {
76
80
  elements: { [name: string]: Element }
77
81
  }
78
82
 
79
- export interface entity extends Omit<struct,'elements'> {
83
+ export interface entity extends Omit<struct, 'elements'> {
84
+
80
85
  /**
81
86
  * Entities with a query signify a view
82
87
  */
83
88
  query?: SELECT
89
+
84
90
  /**
85
91
  * Elements of entities may have additional qualifiers
86
92
  */
87
- elements : EntityElements
93
+ elements: EntityElements
88
94
  // REVISIT: following should move to LinkedCSN
89
95
  keys: { [name: string]: Definition }
90
- drafts: entity
96
+ drafts?: entity
91
97
  }
92
98
 
93
99
  export type EntityElements = {
94
- [name:string]: Element & {
95
- key? : boolean
96
- virtual? : boolean
97
- unique? : boolean
98
- notNull? : boolean
99
- }
100
+ [name: string]: Element & {
101
+ key?: boolean,
102
+ virtual?: boolean,
103
+ unique?: boolean,
104
+ notNull?: boolean,
105
+ },
100
106
  }
101
107
 
102
108
  export interface Association extends type {
103
109
  type: 'cds.Association' | 'cds.Composition'
104
110
  target: FQN
111
+
105
112
  /**
106
- * The specified cardinality. to-one = {max:1}, to-many = {max:'*'}
113
+ * The specified cardinality. to-one = `{max:1}`, to-many = `{max:'*'}`
107
114
  */
108
- cardinality?: { src?: 1; min?: 1 | 0; max?: 1 | '*' }
115
+ cardinality?: { src?: 1, min?: 1 | 0, max?: 1 | '*' }
116
+
109
117
  /**
110
118
  * The parsed on condition in case of unmanaged Associations
111
119
  */
112
120
  on?: predicate
121
+
113
122
  /**
114
123
  * The optionally specified keys in case of managed Associations
115
124
  */
package/apis/env.d.ts CHANGED
@@ -1,25 +1,22 @@
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 : {
9
- build: any,
10
- hana: any,
11
- i18n: any,
12
- mtx: any,
13
- requires: any,
14
- folders: any,
15
- odata: any,
16
- query: any,
17
- sql: any
18
- }
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: {
8
+ build: any,
9
+ hana: any,
10
+ i18n: any,
11
+ mtx: any,
12
+ requires: any,
13
+ folders: any,
14
+ odata: any,
15
+ query: any,
16
+ sql: any,
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