@cap-js/cds-types 0.0.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.
@@ -0,0 +1,126 @@
1
+ import { LinkedDefinition } from './linked'
2
+ import { Query } from './cqn'
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
+ }
30
+
31
+
32
+ /**
33
+ * Represents the invocation context of incoming request and event messages.
34
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
35
+ */
36
+ export class EventContext {
37
+ constructor(properties:{event:string, data?:object, query?:object, headers:object});
38
+ http?: {req: express.Request, res: express.Response}
39
+ tenant: string
40
+ user: User
41
+ id: string
42
+ locale: `${string}_${string}`
43
+ timestamp: Date
44
+ features?: { [key: string]: boolean }
45
+ }
46
+
47
+ /**
48
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
49
+ */
50
+ export class Event extends EventContext {
51
+ event: string
52
+ data: any
53
+ headers: any
54
+ }
55
+
56
+ /**
57
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/events)
58
+ */
59
+ export class Request extends Event {
60
+ params: (string | {})[]
61
+ method: string
62
+ path: string
63
+ target: LinkedDefinition
64
+ /**
65
+ * Shortcut to {@link target.name}
66
+ * @see https://cap.cloud.sap/docs/node.js/events#req-entity
67
+ */
68
+ entity: string
69
+ query: Query
70
+ subject: ref
71
+
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
97
+ }
98
+
99
+
100
+ export class User {
101
+ constructor(obj?: string | { id: string; attr: Record<string, string>; roles: Record<string, string> } | User)
102
+ id: string
103
+
104
+ /**
105
+ * @deprecated Use https://cap.cloud.sap/docs/node.js/events#locale instead
106
+ */
107
+ locale: string
108
+
109
+ /**
110
+ * @deprecated Use https://cap.cloud.sap/docs/node.js/events#tenant instead
111
+ */
112
+ tenant: string | undefined
113
+
114
+ attr: Record<string, string>
115
+ roles: Array<string> | Record<string, string>
116
+ static Privileged: typeof Privileged
117
+ is(role: string): boolean
118
+ }
119
+
120
+ /**
121
+ * Subclass for executing code with superuser privileges.
122
+ */
123
+ declare class Privileged extends User {
124
+ constructor()
125
+ is(): boolean
126
+ }
@@ -0,0 +1,32 @@
1
+ // Types in this file are not part of the API.
2
+ // They are merely meant as definitions that are used
3
+ // in several places within the API.
4
+
5
+
6
+ export interface Constructable<T = any> {
7
+ new(...args: any[]): T
8
+ }
9
+
10
+ // any class (not value) of array to represent plural types used in cds-typer.
11
+ // Mainly used as pattern match for SingularType
12
+ //type ArrayConstructable = Constructable<Array<unknown>>
13
+ export interface ArrayConstructable<T = any> {
14
+ new(...args: any[]): T[]
15
+ }
16
+
17
+ // concrete singular type.
18
+ // `SingularType<typeof Books>` == `Book`.
19
+ export type SingularType<T extends ArrayConstructable<T>> = InstanceType<T>[number]
20
+
21
+ // Convenient way of unwrapping the inner type from array-typed values, as well as the value type itself
22
+ // `class MyArray<T> extends Array<T>``
23
+ // The latter is used heavily in the CDS typer, but its behaviour depends based on how the types are imported:
24
+ // If they are imported on a value based (`require('path/to/type')`) they will be considered `ArrayConstructable`.
25
+ // But if they are being used on type level (JSDOC: `/* @type {import('path/to/type')} */`) they are considered an `Array` .
26
+ // This type introduces an indirection that streamlines their behaviour for both cases.
27
+ // For any scalar type `Unwrap` behaves idempotent.
28
+ export type Unwrap<T> = T extends ArrayConstructable
29
+ ? SingularType<T>
30
+ : T extends Array<infer U>
31
+ ? U
32
+ : T
@@ -0,0 +1,97 @@
1
+ import { CSN, FQN, Association, Definition, entity, kinds } from "./csn"
2
+
3
+ export type LinkedDefinition = linked & Definition & LinkedEntity & LinkedAssociation
4
+ export type Definitions = { [name: string]: LinkedDefinition }
5
+
6
+ export interface linked {
7
+ is(kind: kinds | 'Association' | 'Composition'): boolean
8
+ name: FQN
9
+ }
10
+
11
+ interface LinkedEntity extends linked, entity {
12
+ constructor (properties: object)
13
+ keys: Definitions
14
+ drafts: LinkedEntity
15
+ }
16
+
17
+ interface LinkedAssociation extends linked, Association {
18
+ is2one: boolean
19
+ is2many: boolean
20
+ }
21
+
22
+ export interface LinkedCSN extends CSN {
23
+
24
+ /**
25
+ * Fetches definitions matching the given filter, returning an iterator on them.
26
+ * @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
31
+ */
32
+ each(x: Filter, defs?: Definitions): IterableIterator<any>
33
+
34
+ /**
35
+ * Fetches definitions matching the given filter, returning them in an array.
36
+ * Convenience shortcut for `[...reflect.each('entity')]`
37
+ */
38
+ all(x: Filter, defs?: Definitions): any[]
39
+
40
+ /**
41
+ * Fetches definitions matching the given filter, returning the first match, if any.
42
+ * @example
43
+ * let service = model.find('service')
44
+ * @param {Filter} [x] the filter
45
+ * @param {Definitions} [defs] the definitions to fetch in, default: `this.definitions`
46
+ */
47
+ find(x: Filter, defs?: Definitions): any
48
+
49
+ /**
50
+ * Calls the visitor for each definition matching the given filter.
51
+ * @see [capire](https://github.wdf.sap.corp/pages/cap/node.js/api#cds-reflect-foreach)
52
+ */
53
+ foreach(x: Filter, visitor: Visitor, defs?: Definitions): this
54
+ foreach(visitor: Visitor, defs?: Definitions): this
55
+
56
+ /**
57
+ * Same as foreach but recursively visits each element definition
58
+ * @see [capire](https://github.wdf.sap.corp/pages/cap/node.js/api#cds-reflect-foreach)
59
+ */
60
+ forall(x: Filter, visitor: Visitor, defs?: Definitions): this
61
+ forall(visitor: Visitor, defs?: Definitions): this
62
+
63
+ /**
64
+ * Fetches definitions declared as children of a given parent context or service.
65
+ * It fetches all definitions whose fully-qualified names start with the parent's name.
66
+ * Returns the found definitions as an object with the local names as keys.
67
+ * @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
+ */
73
+ childrenOf(parent: any | string, filter?: ((def: LinkedDefinition) => boolean)): Definitions
74
+
75
+ /**
76
+ * Provides convenient access to the model's top-level definitions.
77
+ * For example, you can use it in an es6-import-like fashion to avoid
78
+ * working with fully-qualified names as follows:
79
+ *
80
+ * @example
81
+ * let model = cds.reflect (cds.parse(`
82
+ * namespace our.lovely.bookshop;
83
+ * entity Books {...}
84
+ * entity Authors {...}
85
+ * `))
86
+ * const {Books,Authors} = model.exports
87
+ * SELECT.from (Books) .where ({ID:11})
88
+ */
89
+ exports: Definitions & ((namespace: string) => Definitions)
90
+ entities: Definitions & ((namespace: string) => Definitions)
91
+ services: Definitions & ((namespace: string) => Definitions)
92
+ definitions: Definitions
93
+
94
+ }
95
+
96
+ type Visitor = (def: LinkedDefinition, name: string, parent: LinkedDefinition, defs: Definitions) => void
97
+ type Filter = string | ((def: LinkedDefinition) => boolean)
package/apis/log.d.ts ADDED
@@ -0,0 +1,165 @@
1
+ export = cds
2
+ declare class cds {
3
+ /**
4
+ * Create a new logger, or install a custom log formatter
5
+ */
6
+ log: LogFactory
7
+
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
+ }
21
+
22
+ declare type LogFactory = {
23
+
24
+ /**
25
+ * Returns a trace logger for the given module if trace is switched on for it,
26
+ * otherwise returns null. All cds runtime packages use this method for their
27
+ * trace and debug output.
28
+ *
29
+ * By default this logger would prefix all output with `[sql] - `
30
+ * You can change this by specifying another prefix in the options:
31
+ *
32
+ * ```
33
+ * const LOG = cds.log('sql|db', { prefix: 'cds.ql' })
34
+ * ```
35
+ *
36
+ * Call `cds.log()` for a given module again to dynamically change the log level
37
+ * of all formerly created loggers, for example:
38
+ *
39
+ * ```
40
+ * const LOG = cds.log('sql')
41
+ * LOG.info ('this will show, as default level is info')
42
+ * cds.log('sql', 'warn')
43
+ * LOG.info('this will be suppressed now')
44
+ * ```
45
+ *
46
+ * @param name logger name
47
+ * @param options level, label and prefix
48
+ * @returns the logger
49
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-log)
50
+ */
51
+ (name: string, options?: string | number | { level?: number, label?: string, prefix?: string }): Logger
52
+
53
+ /**
54
+ * Set a custom formatter function like that:
55
+ * ```
56
+ * cds.log.format = (module, level, ...args) => [ '[', module, ']', ...args ]
57
+ * ```
58
+ *
59
+ * The formatter shall return an array of arguments, which are passed to the logger (for example, `console.log()`)
60
+ */
61
+ format: Formatter
62
+
63
+ /**
64
+ * Set a custom logger.
65
+ * ```
66
+ * cds.log.Logger = ...
67
+ * ```
68
+ */
69
+ Logger: Logger
70
+
71
+ winstonLogger (LoggerOptions?: {level?: string, levels?: any, format?: any, transports?: any, exitOnError?: boolean | Function, silent?: boolean})
72
+ }
73
+
74
+ declare class Logger {
75
+ /**
76
+ * Logs with 'trace' level
77
+ */
78
+ trace: Log
79
+
80
+ /**
81
+ * Logs with 'debug' level
82
+ */
83
+ debug: Log
84
+
85
+ /**
86
+ * Logs with 'info' level
87
+ */
88
+ info: Log
89
+
90
+ /**
91
+ * Logs with 'warn' level
92
+ */
93
+ warn: Log
94
+
95
+ /**
96
+ * Logs with 'error' level
97
+ */
98
+ error: Log
99
+
100
+ /**
101
+ * Logs with default level
102
+ */
103
+ log: Log
104
+
105
+ /**
106
+ * @returns whether 'trace' level is active
107
+ */
108
+ _trace: boolean
109
+
110
+ /**
111
+ * @returns whether 'debug' level is active
112
+ */
113
+ _debug: boolean
114
+
115
+ /**
116
+ * @returns whether 'info' level is active
117
+ */
118
+ _info: boolean
119
+
120
+ /**
121
+ * @returns whether 'warn' level is active
122
+ */
123
+ _warn: boolean
124
+
125
+ /**
126
+ * @returns whether 'error' level is active
127
+ */
128
+ _error: boolean
129
+
130
+ /**
131
+ * Change the format for this logger instance:
132
+ * ```
133
+ * cds.log('foo').setFormat((module, level, ...args) => [ '[', module, ']', ...args ])
134
+ * ```
135
+ *
136
+ * The formatter shall return an array of arguments, which are passed to the logger (for example, `console.log()`)
137
+ */
138
+ setFormat(formatter: Formatter)
139
+ }
140
+
141
+ declare type Formatter = {
142
+ /**
143
+ * Custom format function
144
+ *
145
+ * @param module logger name
146
+ * @param level log level
147
+ * @param args additional arguments
148
+ * @returns an array of arguments, which are passed to the logger (for example, `console.log()`)
149
+ */
150
+ (module: string, level: number, args: any[]): any[]
151
+ }
152
+
153
+ declare type Log = {
154
+ /**
155
+ * Logs a message
156
+ *
157
+ * @param message text to log
158
+ * @param optionalParams additional parameters, same as in `console.log(text, param1, ...)`
159
+ */
160
+ (message?: any, ...optionalParams: any[]): void
161
+ }
162
+
163
+ declare enum levels {
164
+ SILENT = 0, ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4, TRACE = 5, SILLY = 5, VERBOSE = 5
165
+ }
@@ -0,0 +1,180 @@
1
+ import { Query as CQN, expr, _xpr } from "./cqn"
2
+ import { LinkedCSN } from "./linked"
3
+ import { CSN } from "./csn"
4
+
5
+ type _flavor = 'parsed' | 'xtended' | 'inferred'
6
+ type _odata_options = {
7
+ flavor?: 'v2' | 'v4' | 'w4'| 'x4',
8
+ version?: 'v2' | 'v4',
9
+ structs?: boolean,
10
+ refs?: boolean,
11
+ }
12
+ type _options = {
13
+ flavor?: _flavor,
14
+ plain?: boolean,
15
+ docs?: boolean,
16
+ names?: string,
17
+ odata?: _odata_options,
18
+ } | _flavor
19
+
20
+ type JSON = string
21
+ type YAML = string
22
+ type CDL = string
23
+ type SQL = string
24
+ type XML = string
25
+ type EDM = { $version:string }
26
+ type EDMX = XML
27
+ type filename = string
28
+
29
+
30
+ export default class cds {
31
+
32
+ /**
33
+ * The effective CDS model loaded during bootstrapping, which contains all service and entity definitions,
34
+ * including required services.
35
+ */
36
+ model?: LinkedCSN
37
+
38
+ /**
39
+ * Provides a set of methods to parse a given model, query or expression.
40
+ * You can also use `cds.parse()` as a shortcut to `cds.parse.cdl()`.
41
+ */
42
+ parse : {
43
+ /** Shortcut to `cds.parse.cdl()` */
44
+ (cdl:CDL) : CSN
45
+ cdl (cdl:CDL) : CSN
46
+ cql (src:string) : CQN
47
+ expr (src:string) : expr
48
+ xpr (src:string) : _xpr
49
+ ref (src:string) : string[]
50
+ }
51
+
52
+
53
+ /**
54
+ * Provides a set of methods to parse a given model, query or expression.
55
+ * You can also use `cds.compile(csn).to('<output>')` as a fluent variant.
56
+ */
57
+ compile : {
58
+ /** Shortcut for `cds.compile.to.csn()` */
59
+ cdl (model:CDL, o?:_options) : CSN,
60
+
61
+ for: {
62
+ odata (model:CSN, o?:_options) : CSN
63
+ sql (model:CSN, o?:_options) : CSN
64
+ },
65
+ to: {
66
+ parsed:{
67
+ csn (files:filename[], o?:_options) : Promise<CSN>
68
+ csn (model:CDL, o?:_options) : CSN
69
+ }
70
+ xtended:{
71
+ csn (files:filename[], o?:_options) : Promise<CSN>
72
+ csn (model:CDL, o?:_options) : CSN
73
+ }
74
+ inferred:{
75
+ csn (files:filename[], o?:_options) : Promise<CSN>
76
+ csn (model:CDL, o?:_options) : CSN
77
+ }
78
+ csn (files:filename[], o?:_options) : Promise<CSN>
79
+ csn (model:CDL, o?:_options) : CSN
80
+ yml (model:CSN, o?:_options) : YAML
81
+ yaml (model:CSN, o?:_options) : YAML
82
+ json (model:CSN, o?:_options) : JSON
83
+ sql (model:CSN, o?:_options) : SQL[]
84
+ cdl (model:CSN, o?:_options) : CDL | Iterable<[CDL,{file:filename}]>
85
+ edm (model:CSN, o?:_options|_odata_options) : EDM | string
86
+ edmx (model:CSN, o?:_options|_odata_options) : EDMX | Iterable<[EDMX,{file:filename}]>
87
+ hdbcds (model:CSN, o?:_options) : SQL | Iterable<[SQL,{file:filename}]>
88
+ hdbtable (model:CSN, o?:_options) : SQL | Iterable<[SQL,{file:filename}]>
89
+ }
90
+
91
+ /** Fluent API variant */
92
+ (model: CSN | CDL) : {
93
+ for: {
94
+ odata (o?:_options) : CSN
95
+ sql (o?:_options) : CSN
96
+ },
97
+ to: {
98
+ parsed:{ csn (o?:_options) : CSN }
99
+ xtended:{ csn (o?:_options) : CSN }
100
+ inferred:{ csn (o?:_options) : CSN }
101
+ csn (o?:_options) : CSN
102
+ yml (o?:_options) : YAML
103
+ yaml (o?:_options) : YAML
104
+ json (o?:_options) : JSON
105
+ sql (o?:_options) : SQL[]
106
+ cdl (o?:_options) : CDL | Iterable<[CDL,{file:filename}]>
107
+ edm (o?:_options|_odata_options) : EDM | string
108
+ edmx (o?:_options|_odata_options) : EDMX | Iterable<[EDMX,{file:filename}]>
109
+ hdbcds (o?:_options) : SQL | Iterable<[SQL,{file:filename}]>
110
+ hdbtable (o?:_options) : SQL | Iterable<[SQL,{file:filename}]>
111
+ }
112
+ }
113
+
114
+ /** Async fluent variant reading from files */
115
+ (files: filename[]) : {
116
+ for: {
117
+ odata (o?:_options) : Promise<CSN>
118
+ sql (o?:_options) : Promise<CSN>
119
+ },
120
+ to: {
121
+ parsed:{ csn (o?:_options) : Promise <CSN> }
122
+ xtended:{ csn (o?:_options) : Promise <CSN> }
123
+ inferred:{ csn (o?:_options) : Promise <CSN> }
124
+ csn (o?:_options) : Promise <CSN>
125
+ yml (o?:_options) : Promise <YAML>
126
+ yaml (o?:_options) : Promise <YAML>
127
+ json (o?:_options) : Promise <JSON>
128
+ sql (o?:_options) : Promise <SQL[]>
129
+ cdl (o?:_options) : Promise <CDL | Iterable<[CDL,{file:filename}]>>
130
+ edm (o?:_options|_odata_options) : Promise <EDM | string>
131
+ edmx (o?:_options|_odata_options) : Promise <EDMX | Iterable<[EDMX,{file:filename}]>>
132
+ hdbcds (o?:_options) : Promise <SQL | Iterable<[SQL,{file:filename}]>>
133
+ hdbtable (o?:_options) : Promise <SQL | Iterable<[SQL,{file:filename}]>>
134
+ }
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Loads and parses models from the specified files.
140
+ * Uses `cds.resolve` to fetch the respective models.
141
+ * Essentially a shortcut for `cds.compile.to.csn(files)`
142
+ * @param {string} files - filenames of models or if folder containing models
143
+ */
144
+ get(files: '*' | filename | filename[], o?:_options) : Promise<CSN>
145
+
146
+ /**
147
+ * Shortcut for `cds.get(files, 'inferred')`
148
+ * @param {string} files - filenames of models or if folder containing models
149
+ */
150
+ load(files: '*' | filename | filename[], o?:_options) : Promise<CSN>
151
+
152
+ /**
153
+ * Emitted whenever a model is loaded using cds.load().
154
+ */
155
+ on (event : 'loaded', listener : (model : CSN) => void) : this
156
+
157
+
158
+ /**
159
+ * Resolves given file or module name(s) to an array of absolute file names.
160
+ * Uses Node's `require.resolve` internally with the following additions:
161
+ * - relative names are resolved relative to the current working directory instead of the current JavaScript module; hence, use __dirname if you want to find or load models relative to the current module.
162
+ * - if no file extension is given, `.csn` and `.cds` will be appended in that order.
163
+ * @param files - The file or module name(s) of a model or a folder containing models. Specify `'*'` to fetch moels from default locations, i.e. `[ 'db/', 'srv/', 'app/' ]`
164
+ * @returns An array of absolute file names or `undefined` if none could be resolved.
165
+ */
166
+ resolve (files: '*' | filename | filename[]) : filename[] | undefined
167
+
168
+ /**
169
+ * Turns the given plain CSN model into a linked model
170
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect)
171
+ */
172
+ linked(model: CSN): LinkedCSN
173
+
174
+ /**
175
+ * Turns the given plain CSN model into a reflected model
176
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect)
177
+ */
178
+ reflect(model: CSN): LinkedCSN
179
+
180
+ }