UNPKG

2.96 kBPlain TextView Raw
1// Copyright IBM Corp. 2017,2020. All Rights Reserved.
2// Node module: @loopback/repository
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6/**
7 * Common types/interfaces such as Class/Constructor/Options/Callback
8 */
9/* eslint-disable @typescript-eslint/no-explicit-any */
10
11/**
12 * Interface for classes with `new` operator and static properties/methods
13 */
14export interface Class<T> {
15 // new MyClass(...args) ==> T
16 new (...args: any[]): T;
17 // Other static properties/operations
18 [property: string]: any;
19}
20
21/**
22 * Interface for constructor functions without `new` operator.
23 *
24 * @example
25 * ```ts
26 * function Foo(x) {
27 * if (!(this instanceof Foo)) { return new Foo(x); }
28 * this.x = x;
29 * }
30 * ```
31 */
32export interface ConstructorFunction<T> {
33 (...args: any[]): T;
34}
35
36/**
37 * Constructor type - class or function
38 */
39export type Constructor<T> = Class<T> | ConstructorFunction<T>;
40
41/**
42 * Objects with open properties
43 */
44export interface AnyObject {
45 [property: string]: any;
46}
47
48/**
49 * An extension of the built-in Partial<T> type which allows partial values
50 * in deeply nested properties too.
51 */
52export type DeepPartial<T> =
53 | Partial<T> // handle free-form properties, e.g. DeepPartial<AnyObject>
54 | {[P in keyof T]?: DeepPartial<T[P]>};
55
56/**
57 * Type alias for strongly or weakly typed objects of T
58 */
59export type DataObject<T extends object> = T | DeepPartial<T>;
60
61/**
62 * Type alias for Node.js options object
63 */
64export type Options = AnyObject;
65
66/**
67 * Type alias for Node.js callback functions
68 */
69export type Callback<T> = (
70 err: Error | string | null | undefined,
71 result?: T,
72) => void;
73
74/**
75 * Type for a command
76 */
77export type Command = string | AnyObject;
78
79/**
80 * Named parameters, such as `{x: 1, y: 'a'}`
81 */
82export type NamedParameters = AnyObject;
83
84/**
85 * Positional parameters, such as [1, 'a']
86 */
87export type PositionalParameters = any[];
88
89/**
90 * Count of Model instances that were successful for methods like `updateAll`,
91 * `deleteAll`, etc.
92 */
93export interface Count {
94 count: number;
95}
96
97/**
98 * JSON Schema describing the Count interface. It's the response type for
99 * REST calls to APIs which return `count`. The type is compatible with
100 * `SchemaObject` from `@loopback/openapi-v3`, which is not an explicit
101 * dependency for `@loopback/repository`.
102 */
103export const CountSchema /* :SchemaObject */ = {
104 type: 'object' as const, // Force to be `object` type instead of `string`
105 title: 'loopback.Count',
106 'x-typescript-type': '@loopback/repository#Count',
107 properties: {
108 count: {
109 type: 'number' as const, // Force to be `number` type instead of `string`
110 },
111 },
112};
113
114/**
115 * Type helper to infer prototype from a constructor function.
116 *
117 * Example: `PrototypeOf<typeof Entity>` is resolved to `Entity`.
118 */
119export type PrototypeOf<Ctor extends Function> = Ctor extends {
120 prototype: infer Proto;
121}
122 ? Proto
123 : never;