@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/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,71 +9,112 @@ 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});
38
- http?: {req: express.Request, res: express.Response}
12
+
13
+ constructor (properties: { event: string, data?: object, query?: object, headers?: object })
14
+ http?: { req: express.Request, res: express.Response }
15
+
39
16
  tenant: string
17
+
40
18
  user: User
19
+
41
20
  id: string
21
+
42
22
  locale: `${string}_${string}`
23
+
43
24
  timestamp: Date
25
+
44
26
  features?: { [key: string]: boolean }
27
+
45
28
  }
46
29
 
47
30
  /**
48
31
  * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
49
32
  */
50
33
  export class Event extends EventContext {
34
+
51
35
  event: string
36
+
52
37
  data: any
38
+
53
39
  headers: any
40
+
54
41
  }
55
42
 
56
43
  /**
57
44
  * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
58
45
  */
59
46
  export class Request extends Event {
60
- params: (string | {})[]
47
+
48
+ params: (string | object)[]
49
+
61
50
  method: string
51
+
62
52
  path: string
53
+
63
54
  target: LinkedDefinition
55
+
64
56
  /**
65
- * Shortcut to {@link target.name}
57
+ * Shortcut to {@link Request.target | target (entity) name}
66
58
  * @see https://cap.cloud.sap/docs/node.js/events#req-entity
67
59
  */
68
60
  entity: string
61
+
69
62
  query: Query
63
+
70
64
  subject: ref
71
65
 
72
- reply(results: any): void
73
-
74
- notify(code: number, message: string, target?: string, args?: any[]): Error
75
- info(code: number, message: string, target?: string, args?: any[]): Error
76
- warn(code: number, message: string, target?: string, args?: any[]): Error
77
- error(code: number, message: string, target?: string, args?: any[]): Error
78
- reject(code: number, message: string, target?: string, args?: any[]): Error
79
-
80
- notify(code: number, message: string, args?: any[]): Error
81
- info(code: number, message: string, args?: any[]): Error
82
- warn(code: number, message: string, args?: any[]): Error
83
- error(code: number, message: string, args?: any[]): Error
84
- reject(code: number, message: string, args?: any[]): Error
85
-
86
- notify(message: string, target?: string, args?: any[]): Error
87
- info(message: string, target?: string, args?: any[]): Error
88
- warn(message: string, target?: string, args?: any[]): Error
89
- error(message: string, target?: string, args?: any[]): Error
90
- reject(message: string, target?: string, args?: any[]): Error
91
-
92
- notify(message: { code?: number | string; message: string; target?: string; args?: any[] }): Error
93
- info(message: { code?: number | string; message: string; target?: string; args?: any[] }): Error
94
- warn(message: { code?: number | string; message: string; target?: string; args?: any[] }): Error
95
- error(message: { code?: number | string; message: string; target?: string; args?: any[], status?: number }): Error
96
- reject(message: { code?: number | string; message: string; target?: string; args?: any[], status?: number }): Error
66
+ reply (results: any): void
67
+
68
+ notify (code: number, message: string, target?: string, args?: any[]): Error
69
+
70
+ info (code: number, message: string, target?: string, args?: any[]): Error
71
+
72
+ warn (code: number, message: string, target?: string, args?: any[]): Error
73
+
74
+ error (code: number, message: string, target?: string, args?: any[]): Error
75
+
76
+ reject (code: number, message: string, target?: string, args?: any[]): Error
77
+
78
+ notify (code: number, message: string, args?: any[]): Error
79
+
80
+ info (code: number, message: string, args?: any[]): Error
81
+
82
+ warn (code: number, message: string, args?: any[]): Error
83
+
84
+ error (code: number, message: string, args?: any[]): Error
85
+
86
+ reject (code: number, message: string, args?: any[]): Error
87
+
88
+ notify (message: string, target?: string, args?: any[]): Error
89
+
90
+ info (message: string, target?: string, args?: any[]): Error
91
+
92
+ warn (message: string, target?: string, args?: any[]): Error
93
+
94
+ error (message: string, target?: string, args?: any[]): Error
95
+
96
+ reject (message: string, target?: string, args?: any[]): Error
97
+
98
+ notify (message: { code?: number | string, message: string, target?: string, args?: any[] }): Error
99
+
100
+ info (message: { code?: number | string, message: string, target?: string, args?: any[] }): Error
101
+
102
+ warn (message: { code?: number | string, message: string, target?: string, args?: any[] }): Error
103
+
104
+ error (message: { code?: number | string, message: string, target?: string, args?: any[], status?: number }): Error
105
+
106
+ reject (message: { code?: number | string, message: string, target?: string, args?: any[], status?: number }): Error
107
+
97
108
  }
98
109
 
99
110
 
111
+ /**
112
+ * Represents the user in a given context.
113
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/authentication#cds-user)
114
+ */
100
115
  export class User {
101
- constructor(obj?: string | { id: string; attr: Record<string, string>; roles: Record<string, string> } | User)
116
+
117
+ constructor (obj?: string | { id: string, attr: Record<string, string>, roles: Array<string> | Record<string, string> } | User)
102
118
  id: string
103
119
 
104
120
  /**
@@ -112,15 +128,22 @@ export class User {
112
128
  tenant: string | undefined
113
129
 
114
130
  attr: Record<string, string>
131
+
115
132
  roles: Array<string> | Record<string, string>
133
+
116
134
  static Privileged: typeof Privileged
117
- is(role: string): boolean
135
+
136
+ is (role: string): boolean
137
+
118
138
  }
119
139
 
120
140
  /**
121
141
  * Subclass for executing code with superuser privileges.
122
142
  */
123
143
  declare class Privileged extends User {
124
- constructor()
125
- is(): boolean
144
+
145
+ constructor ()
146
+
147
+ is (): boolean
148
+
126
149
  }
@@ -4,14 +4,14 @@
4
4
 
5
5
 
6
6
  export interface Constructable<T = any> {
7
- new(...args: any[]): T
7
+ new(...args: any[]): T
8
8
  }
9
9
 
10
10
  // any class (not value) of array to represent plural types used in cds-typer.
11
11
  // Mainly used as pattern match for SingularType
12
- //type ArrayConstructable = Constructable<Array<unknown>>
12
+ // type ArrayConstructable = Constructable<Array<any>>
13
13
  export interface ArrayConstructable<T = any> {
14
- new(...args: any[]): T[]
14
+ new(...args: any[]): T[]
15
15
  }
16
16
 
17
17
  // concrete singular type.
@@ -28,5 +28,39 @@ export type SingularType<T extends ArrayConstructable<T>> = InstanceType<T>[numb
28
28
  export type Unwrap<T> = T extends ArrayConstructable
29
29
  ? SingularType<T>
30
30
  : T extends Array<infer U>
31
- ? U
32
- : T
31
+ ? U
32
+ : T
33
+
34
+
35
+ /*
36
+ * the following three types are used to convert union types to intersection types.
37
+ * We need these as our types currently lack generics in places where we would need them to clearly decide
38
+ * on a subtype in the case of a union type. This leads to the following problem:
39
+ *
40
+ * ```ts
41
+ * type A = { a: number }
42
+ * type B = { b: string }
43
+ * type Foo = A | B
44
+ * function f(): Foo { ... }
45
+ * const x = f()
46
+ * x.a // error, could also be B
47
+ * ```
48
+ *
49
+ * While we should have:
50
+ *
51
+ * ```ts
52
+ * function f<T extends Foo>(): T { ... }
53
+ * const x = f<A>()
54
+ * x.a
55
+ * ```ts
56
+ *
57
+ * Since we don't do that yet, we opt for intersection types instead.
58
+ * By also wrapping it in Partial, we at least force the user to check for the presence of any
59
+ * attribute they try to access.
60
+ *
61
+ * Places where these types are used are subject to a rework!
62
+ * the idea behind the conversion can be found in this excellent writeup: https://fettblog.eu/typescript-union-to-intersection/
63
+ */
64
+ export type Scalarise<A> = A extends Array<infer N> ? N : A
65
+ export type UnionToIntersection<U> = Partial<(U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never>
66
+ export type UnionsToIntersections<U> = Array<UnionToIntersection<Scalarise<U>>>
package/apis/linked.d.ts CHANGED
@@ -1,83 +1,89 @@
1
- import { CSN, FQN, Association, Definition, entity, kinds } from "./csn"
1
+ 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
- is(kind: kinds | 'Association' | 'Composition'): boolean
8
- name: FQN
9
+ is(kind: kinds | 'Association' | 'Composition'): boolean
10
+ name: FQN
9
11
  }
10
12
 
11
13
  interface LinkedEntity extends linked, entity {
12
- constructor (properties: object)
13
- keys: Definitions
14
- drafts: LinkedEntity
14
+ keys: Definitions
15
+ drafts?: LinkedEntity
15
16
  }
16
17
 
17
18
  interface LinkedAssociation extends linked, Association {
18
- is2one: boolean
19
- is2many: boolean
19
+ is2one: boolean
20
+ is2many: boolean
20
21
  }
21
22
 
22
23
  export interface LinkedCSN extends CSN {
23
24
 
24
- /**
25
+ /**
25
26
  * Fetches definitions matching the given filter, returning an iterator on them.
26
27
  * @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
28
+ * ```js
29
+ * let m = cds.reflect (aParsedModel)
30
+ * for (let d of m.each('entity')) console.log (d.kind, d.name)
31
+ * let entities = [...m.each('entity')] //> capture all
32
+ * let entities = m.all('entity') //> equivalent shortcut
33
+ * ```
31
34
  */
32
- each(x: Filter, defs?: Definitions): IterableIterator<any>
35
+ each(x: Filter, defs?: Definitions): IterableIterator<any>
33
36
 
34
- /**
37
+ /**
35
38
  * Fetches definitions matching the given filter, returning them in an array.
36
39
  * Convenience shortcut for `[...reflect.each('entity')]`
37
40
  */
38
- all(x: Filter, defs?: Definitions): any[]
41
+ all(x: Filter, defs?: Definitions): any[]
39
42
 
40
- /**
43
+ /**
41
44
  * Fetches definitions matching the given filter, returning the first match, if any.
42
45
  * @example
43
46
  * let service = model.find('service')
44
- * @param {Filter} [x] the filter
45
- * @param {Definitions} [defs] the definitions to fetch in, default: `this.definitions`
47
+ * @param x - the filter
48
+ * @param defs - the definitions to fetch in, default: `this.definitions`
46
49
  */
47
- find(x: Filter, defs?: Definitions): any
50
+ find(x: Filter, defs?: Definitions): any
48
51
 
49
- /**
52
+ /**
50
53
  * Calls the visitor for each definition matching the given filter.
51
54
  * @see [capire](https://github.wdf.sap.corp/pages/cap/node.js/api#cds-reflect-foreach)
52
55
  */
53
- foreach(x: Filter, visitor: Visitor, defs?: Definitions): this
54
- foreach(visitor: Visitor, defs?: Definitions): this
56
+ foreach(x: Filter, visitor: Visitor, defs?: Definitions): this
57
+ foreach(visitor: Visitor, defs?: Definitions): this
55
58
 
56
- /**
59
+ /**
57
60
  * Same as foreach but recursively visits each element definition
58
61
  * @see [capire](https://github.wdf.sap.corp/pages/cap/node.js/api#cds-reflect-foreach)
59
62
  */
60
- forall(x: Filter, visitor: Visitor, defs?: Definitions): this
61
- forall(visitor: Visitor, defs?: Definitions): this
63
+ forall(x: Filter, visitor: Visitor, defs?: Definitions): this
64
+ forall(visitor: Visitor, defs?: Definitions): this
62
65
 
63
- /**
66
+ /**
64
67
  * Fetches definitions declared as children of a given parent context or service.
65
68
  * It fetches all definitions whose fully-qualified names start with the parent's name.
66
69
  * Returns the found definitions as an object with the local names as keys.
67
70
  * @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
71
+ * ```js
72
+ * let service = model.find ('service')
73
+ * let entities = m.childrenOf (service)
74
+ * ```
75
+ * @param parent - either the parent itself or its fully-qualified name
76
+ * @param filter - an optional filter to apply before picking a child
72
77
  */
73
- childrenOf(parent: any | string, filter?: ((def: LinkedDefinition) => boolean)): Definitions
78
+ childrenOf(parent: any | string, filter?: ((def: LinkedDefinition) => boolean)): Definitions
74
79
 
75
- /**
80
+ /**
76
81
  * Provides convenient access to the model's top-level definitions.
77
82
  * For example, you can use it in an es6-import-like fashion to avoid
78
83
  * working with fully-qualified names as follows:
79
84
  *
80
85
  * @example
86
+ * ```js
81
87
  * let model = cds.reflect (cds.parse(`
82
88
  * namespace our.lovely.bookshop;
83
89
  * entity Books {...}
@@ -85,11 +91,12 @@ export interface LinkedCSN extends CSN {
85
91
  * `))
86
92
  * const {Books,Authors} = model.exports
87
93
  * SELECT.from (Books) .where ({ID:11})
94
+ * ```
88
95
  */
89
- exports: Definitions & ((namespace: string) => Definitions)
90
- entities: Definitions & ((namespace: string) => Definitions)
91
- services: Definitions & ((namespace: string) => Definitions)
92
- definitions: Definitions
96
+ exports: Definitions & ((namespace: string) => Definitions)
97
+ entities: Definitions & ((namespace: string) => Definitions)
98
+ services: Definitions & ((namespace: string) => Definitions)
99
+ definitions: Definitions
93
100
 
94
101
  }
95
102
 
package/apis/log.d.ts CHANGED
@@ -1,133 +1,136 @@
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
 
24
- /**
23
+ /**
25
24
  * Returns a trace logger for the given module if trace is switched on for it,
26
25
  * otherwise returns null. All cds runtime packages use this method for their
27
26
  * trace and debug output.
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
  */
51
- (name: string, options?: string | number | { level?: number, label?: string, prefix?: string }): Logger
50
+ (name: string, options?: string | number | { level?: number, label?: string, prefix?: string }): Logger,
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
- format: Formatter
62
-
63
- /**
60
+ format: Formatter,
61
+
62
+ /**
64
63
  * Set a custom logger.
65
- * ```
64
+ * ```js
66
65
  * cds.log.Logger = ...
67
66
  * ```
68
67
  */
69
- Logger: Logger
68
+ Logger: Logger,
70
69
 
71
- winstonLogger (LoggerOptions?: {level?: string, levels?: any, format?: any, transports?: any, exitOnError?: boolean | Function, silent?: boolean})
70
+ // FIXME
71
+ /* eslint-disable-next-line @typescript-eslint/ban-types */
72
+ winstonLogger (LoggerOptions?: { level?: string, levels?: any, format?: any, transports?: any, exitOnError?: boolean | Function, silent?: boolean }),
72
73
  }
73
74
 
74
75
  declare class Logger {
75
- /**
76
- * Logs with 'trace' level
77
- */
78
- trace: Log
79
76
 
80
- /**
77
+
78
+ /**
79
+ * Logs with 'trace' level
80
+ */
81
+ trace: Log
82
+
83
+ /**
81
84
  * Logs with 'debug' level
82
85
  */
83
- debug: Log
86
+ debug: Log
84
87
 
85
- /**
88
+ /**
86
89
  * Logs with 'info' level
87
90
  */
88
- info: Log
91
+ info: Log
89
92
 
90
- /**
93
+ /**
91
94
  * Logs with 'warn' level
92
95
  */
93
- warn: Log
96
+ warn: Log
94
97
 
95
- /**
98
+ /**
96
99
  * Logs with 'error' level
97
100
  */
98
- error: Log
101
+ error: Log
99
102
 
100
- /**
103
+ /**
101
104
  * Logs with default level
102
105
  */
103
- log: Log
106
+ log: Log
104
107
 
105
- /**
108
+ /**
106
109
  * @returns whether 'trace' level is active
107
110
  */
108
- _trace: boolean
111
+ _trace: boolean
109
112
 
110
- /**
113
+ /**
111
114
  * @returns whether 'debug' level is active
112
115
  */
113
- _debug: boolean
116
+ _debug: boolean
114
117
 
115
- /**
118
+ /**
116
119
  * @returns whether 'info' level is active
117
120
  */
118
- _info: boolean
121
+ _info: boolean
119
122
 
120
- /**
123
+ /**
121
124
  * @returns whether 'warn' level is active
122
125
  */
123
- _warn: boolean
126
+ _warn: boolean
124
127
 
125
- /**
128
+ /**
126
129
  * @returns whether 'error' level is active
127
130
  */
128
- _error: boolean
131
+ _error: boolean
129
132
 
130
- /**
133
+ /**
131
134
  * Change the format for this logger instance:
132
135
  * ```
133
136
  * cds.log('foo').setFormat((module, level, ...args) => [ '[', module, ']', ...args ])
@@ -135,31 +138,36 @@ declare class Logger {
135
138
  *
136
139
  * The formatter shall return an array of arguments, which are passed to the logger (for example, `console.log()`)
137
140
  */
138
- setFormat(formatter: Formatter)
141
+ setFormat (formatter: Formatter)
142
+
139
143
  }
140
144
 
141
145
  declare type Formatter = {
142
- /**
146
+
147
+ /**
143
148
  * Custom format function
144
149
  *
145
- * @param module logger name
146
- * @param level log level
147
- * @param args additional arguments
150
+ * @param module - logger name
151
+ * @param level - log level
152
+ * @param args - additional arguments
148
153
  * @returns an array of arguments, which are passed to the logger (for example, `console.log()`)
149
154
  */
150
- (module: string, level: number, args: any[]): any[]
155
+ (module: string, level: number, args: any[]): any[],
151
156
  }
152
157
 
153
158
  declare type Log = {
154
- /**
159
+
160
+ /**
155
161
  * Logs a message
156
162
  *
157
- * @param message text to log
158
- * @param optionalParams additional parameters, same as in `console.log(text, param1, ...)`
163
+ * @param message - text to log
164
+ * @param optionalParams - additional parameters, same as in `console.log(text, param1, ...)`
159
165
  */
160
- (message?: any, ...optionalParams: any[]): void
166
+ (message?: any, ...optionalParams: any[]): void,
161
167
  }
162
168
 
163
169
  declare enum levels {
164
- SILENT = 0, ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4, TRACE = 5, SILLY = 5, VERBOSE = 5
170
+ // FIXME: check if this is a copy-paste error
171
+ /* eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values */
172
+ SILENT = 0, ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4, TRACE = 5, SILLY = 5, VERBOSE = 5
165
173
  }