@faasjs/func 8.0.0-beta.5 → 8.0.0-beta.7

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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { Logger } from '@faasjs/logger';
1
+ import { Logger } from "@faasjs/logger";
2
2
 
3
+ //#region src/utils.d.ts
3
4
  /**
4
5
  * Assigns a name to a given function handler, which will be displayed in logs and error messages.
5
6
  *
@@ -20,69 +21,50 @@ import { Logger } from '@faasjs/logger';
20
21
  * ```
21
22
  */
22
23
  declare function nameFunc<T extends (...args: any[]) => any>(name: string, handler: T): T;
23
-
24
- /**
25
- * FaasJS's function module.
26
- *
27
- * [![License: MIT](https://img.shields.io/npm/l/@faasjs/func.svg)](https://github.com/faasjs/faasjs/blob/main/packages/func/LICENSE)
28
- * [![NPM Version](https://img.shields.io/npm/v/@faasjs/func.svg)](https://www.npmjs.com/package/@faasjs/func)
29
- *
30
- * ## Install
31
- *
32
- * ```sh
33
- * npm install @faasjs/func
34
- * ```
35
- *
36
- * ## Usage
37
- *
38
- * @see {@link useFunc}
39
- *
40
- * @packageDocumentation
41
- */
42
-
24
+ //#endregion
25
+ //#region src/index.d.ts
43
26
  type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
44
27
  type Next = () => Promise<void>;
45
28
  type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event?: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
46
29
  type Plugin = {
47
- [key: string]: any;
48
- readonly type: string;
49
- readonly name: string;
50
- onMount?: (data: MountData, next: Next) => Promise<void>;
51
- onInvoke?: (data: InvokeData, next: Next) => Promise<void>;
30
+ [key: string]: any;
31
+ readonly type: string;
32
+ readonly name: string;
33
+ onMount?: (data: MountData, next: Next) => Promise<void>;
34
+ onInvoke?: (data: InvokeData, next: Next) => Promise<void>;
52
35
  };
53
36
  type Config = {
54
- [key: string]: any;
55
- plugins?: {
56
- [key: string]: {
57
- [key: string]: any;
58
- type?: string;
59
- config?: {
60
- [key: string]: any;
61
- };
62
- };
37
+ [key: string]: any;
38
+ plugins?: {
39
+ [key: string]: {
40
+ [key: string]: any;
41
+ type?: string;
42
+ config?: {
43
+ [key: string]: any;
44
+ };
63
45
  };
46
+ };
64
47
  };
65
48
  type MountData = {
66
- [key: string]: any;
67
- config: Config;
68
- event: any;
69
- context: any;
49
+ [key: string]: any;
50
+ config: Config;
51
+ event: any;
52
+ context: any;
70
53
  };
71
54
  type InvokeData<TEvent = any, TContext = any, TResult = any> = {
72
- [key: string]: any;
73
- event: TEvent;
74
- context: TContext;
75
- callback: any;
76
- response: any;
77
- logger: Logger;
78
- handler?: Handler<TEvent, TContext, TResult>;
79
- config: Config;
55
+ [key: string]: any;
56
+ event: TEvent;
57
+ context: TContext;
58
+ callback: any;
59
+ response: any;
60
+ logger: Logger;
61
+ handler?: Handler<TEvent, TContext, TResult>;
62
+ config: Config;
80
63
  };
81
64
  type LifeCycleKey = 'onMount' | 'onInvoke';
82
65
  type FuncConfig<TEvent = any, TContext = any, TResult = any> = {
83
- plugins?: Plugin[];
84
- handler?: Handler<TEvent, TContext, TResult>;
85
- autoLoadPluginsFromConfig?: boolean;
66
+ plugins?: Plugin[];
67
+ handler?: Handler<TEvent, TContext, TResult>;
86
68
  };
87
69
  /**
88
70
  * Get the event type of a func
@@ -110,54 +92,69 @@ type FuncEventType<T extends Func<any, any, any>> = T extends Func<infer P, any,
110
92
  * ```
111
93
  */
112
94
  type FuncReturnType<T extends Func<any, any, any>> = T extends Func<any, any, infer R> ? R : any;
95
+ type NormalizePluginType<TType extends string> = TType extends `npm:${infer Name}` ? Name : TType extends `@faasjs/${infer Name}` ? Name : TType;
96
+ type UnionToIntersection<T> = (T extends unknown ? (arg: T) => void : never) extends ((arg: infer TResult) => void) ? TResult : never;
97
+ type Simplify<T> = { [K in keyof T]: T[K] } & {};
98
+ /**
99
+ * Plugin event augmentation map.
100
+ *
101
+ * Extend this interface in plugin packages to describe which event fields are
102
+ * injected when the plugin is enabled through `faas.yaml`.
103
+ */
104
+ interface FaasPluginEventMap {}
105
+ type ResolvePluginEvent<TType extends string> = NormalizePluginType<TType> extends keyof FaasPluginEventMap ? FaasPluginEventMap[NormalizePluginType<TType>] : Record<never, never>;
106
+ /**
107
+ * Infer event type from plugin type names.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * type Event = InferPluginEvent<['http']>
112
+ * ```
113
+ */
114
+ type InferPluginEvent<TPlugins extends readonly string[]> = Simplify<Record<string, any> & UnionToIntersection<ResolvePluginEvent<TPlugins[number]>>>;
113
115
  declare function parseFuncFilenameFromStack(stack?: string): string | undefined;
114
116
  declare class Func<TEvent = any, TContext = any, TResult = any> {
115
- [key: string]: any;
116
- plugins: Plugin[];
117
- handler?: Handler<TEvent, TContext, TResult>;
118
- config: Config;
119
- mounted: boolean;
120
- filename?: string;
121
- private readonly autoLoadPluginsFromConfig;
122
- private loadedConfigPlugins;
123
- private cachedFunctions;
124
- /**
125
- * Create a cloud function
126
- * @param config {object} config
127
- * @param config.plugins {Plugin[]} plugins list
128
- * @param config.handler {Handler} business logic
129
- */
130
- constructor(config: FuncConfig<TEvent, TContext>);
131
- private insertPluginBeforeRunHandler;
132
- private resolvePluginConstructor;
133
- private loadPluginsFromConfig;
134
- private compose;
135
- /**
136
- * First time mount the function
137
- */
138
- mount(data?: {
139
- event: TEvent;
140
- context: TContext;
141
- config?: Config;
142
- logger?: Logger;
143
- }): Promise<void>;
144
- /**
145
- * Invoke the function
146
- * @param data {object} data
147
- */
148
- invoke(data: InvokeData<TEvent, TContext, TResult>): Promise<void>;
149
- /**
150
- * Export the function
151
- */
152
- export(): {
153
- handler: ExportedHandler<TEvent, TContext, TResult>;
154
- };
117
+ [key: string]: any;
118
+ plugins: Plugin[];
119
+ handler?: Handler<TEvent, TContext, TResult>;
120
+ config: Config;
121
+ mounted: boolean;
122
+ filename?: string;
123
+ private cachedFunctions;
124
+ /**
125
+ * Create a cloud function
126
+ * @param config {object} config
127
+ * @param config.plugins {Plugin[]} plugins list
128
+ * @param config.handler {Handler} business logic
129
+ */
130
+ constructor(config: FuncConfig<TEvent, TContext>);
131
+ private compose;
132
+ /**
133
+ * First time mount the function
134
+ */
135
+ mount(data?: {
136
+ event: TEvent;
137
+ context: TContext;
138
+ config?: Config;
139
+ logger?: Logger;
140
+ }): Promise<void>;
141
+ /**
142
+ * Invoke the function
143
+ * @param data {object} data
144
+ */
145
+ invoke(data: InvokeData<TEvent, TContext, TResult>): Promise<void>;
146
+ /**
147
+ * Export the function
148
+ */
149
+ export(): {
150
+ handler: ExportedHandler<TEvent, TContext, TResult>;
151
+ };
155
152
  }
156
153
  type UseifyPlugin<T> = T & {
157
- mount: (data?: MountData) => Promise<T>;
154
+ mount: (data?: MountData) => Promise<T>;
158
155
  };
159
156
  declare function usePlugin<T extends Plugin>(plugin: T & {
160
- mount?: (data?: MountData) => Promise<T>;
157
+ mount?: (data?: MountData) => Promise<T>;
161
158
  }): UseifyPlugin<T>;
162
159
  /**
163
160
  * Create a cloud function.
@@ -186,15 +183,5 @@ declare function usePlugin<T extends Plugin>(plugin: T & {
186
183
  * ```
187
184
  */
188
185
  declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
189
- /**
190
- * Create a cloud function from business logic and auto-load plugins from
191
- * `func.config.plugins`.
192
- *
193
- * `defineFunc` receives business logic directly (no wrapper function), and
194
- * resolves plugin modules during the first mount based on config from
195
- * `faas.yaml` (already loaded into `func.config` by `@faasjs/load`,
196
- * `@faasjs/server`, or `@faasjs/dev`).
197
- */
198
- declare function defineFunc<TEvent = any, TContext = any, TResult = any>(handler: Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
199
-
200
- export { type Config, type ExportedHandler, Func, type FuncConfig, type FuncEventType, type FuncReturnType, type Handler, type InvokeData, type LifeCycleKey, type MountData, type Next, type Plugin, type UseifyPlugin, defineFunc, nameFunc, parseFuncFilenameFromStack, useFunc, usePlugin };
186
+ //#endregion
187
+ export { Config, ExportedHandler, FaasPluginEventMap, Func, FuncConfig, FuncEventType, FuncReturnType, Handler, InferPluginEvent, InvokeData, LifeCycleKey, MountData, Next, NormalizePluginType, Plugin, ResolvePluginEvent, Simplify, UnionToIntersection, UseifyPlugin, nameFunc, parseFuncFilenameFromStack, useFunc, usePlugin };