@faasjs/func 0.0.3-beta.86 → 0.0.3-beta.87

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,156 @@
1
+ import { Logger } from '@faasjs/logger';
2
+
3
+ type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
4
+ type Next = () => Promise<void>;
5
+ type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
6
+ type Plugin = {
7
+ [key: string]: any;
8
+ readonly type: string;
9
+ readonly name: string;
10
+ onDeploy?: (data: DeployData, next: Next) => void | Promise<void>;
11
+ onMount?: (data: MountData, next: Next) => void | Promise<void>;
12
+ onInvoke?: (data: InvokeData, next: Next) => void | Promise<void>;
13
+ };
14
+ type ProviderConfig = {
15
+ type: string;
16
+ config: {
17
+ [key: string]: any;
18
+ };
19
+ };
20
+ type Config = {
21
+ [key: string]: any;
22
+ providers?: {
23
+ [key: string]: ProviderConfig;
24
+ };
25
+ plugins?: {
26
+ [key: string]: {
27
+ [key: string]: any;
28
+ provider?: string | ProviderConfig;
29
+ type: string;
30
+ config?: {
31
+ [key: string]: any;
32
+ };
33
+ };
34
+ };
35
+ };
36
+ type DeployData = {
37
+ [key: string]: any;
38
+ root: string;
39
+ filename: string;
40
+ env?: string;
41
+ name?: string;
42
+ config?: Config;
43
+ version?: string;
44
+ dependencies: {
45
+ [name: string]: string;
46
+ };
47
+ plugins?: {
48
+ [name: string]: {
49
+ [key: string]: any;
50
+ name?: string;
51
+ type: string;
52
+ provider?: string;
53
+ config: {
54
+ [key: string]: any;
55
+ };
56
+ plugin: Plugin;
57
+ };
58
+ };
59
+ logger?: Logger;
60
+ };
61
+ type MountData = {
62
+ [key: string]: any;
63
+ config: Config;
64
+ event: any;
65
+ context: any;
66
+ };
67
+ type InvokeData<TEvent = any, TContext = any, TResult = any> = {
68
+ [key: string]: any;
69
+ event: TEvent;
70
+ context: TContext;
71
+ callback: any;
72
+ response: any;
73
+ logger: Logger;
74
+ handler?: Handler<TEvent, TContext, TResult>;
75
+ config: Config;
76
+ };
77
+ type LifeCycleKey = 'onDeploy' | 'onMount' | 'onInvoke';
78
+ type FuncConfig<TEvent = any, TContext = any, TResult = any> = {
79
+ plugins?: Plugin[];
80
+ handler?: Handler<TEvent, TContext, TResult>;
81
+ };
82
+ declare class Func<TEvent = any, TContext = any, TResult = any> {
83
+ [key: string]: any;
84
+ plugins: Plugin[];
85
+ handler?: Handler<TEvent, TContext, TResult>;
86
+ config: Config;
87
+ mounted: boolean;
88
+ filename?: string;
89
+ private cachedFunctions;
90
+ /**
91
+ * Create a cloud function
92
+ * @param config {object} config
93
+ * @param config.plugins {Plugin[]} plugins list
94
+ * @param config.handler {Handler} business logic
95
+ */
96
+ constructor(config: FuncConfig<TEvent, TContext>);
97
+ private compose;
98
+ /**
99
+ * Deploy the function
100
+ * @param data {object} data
101
+ * @param data.root {string} root path
102
+ * @param data.filename {string} filename
103
+ * @param data.env {string} environment
104
+ */
105
+ deploy(data: DeployData): any;
106
+ /**
107
+ * First time mount the function
108
+ */
109
+ mount(data: {
110
+ event: TEvent;
111
+ context: TContext;
112
+ config?: Config;
113
+ logger?: Logger;
114
+ }): Promise<void>;
115
+ /**
116
+ * Invoke the function
117
+ * @param data {object} data
118
+ */
119
+ invoke(data: InvokeData<TEvent, TContext, TResult>): Promise<void>;
120
+ /**
121
+ * Export the function
122
+ */
123
+ export(): {
124
+ handler: ExportedHandler<TEvent, TContext, TResult>;
125
+ };
126
+ }
127
+ type UseifyPlugin<T> = T & {
128
+ mount?: (data?: {
129
+ config?: Config;
130
+ }) => Promise<T>;
131
+ };
132
+ declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlugin<T>;
133
+ /**
134
+ * ```ts
135
+ * // pure function
136
+ * export default useFunc(() => {
137
+ * return () => {
138
+ * return 'Hello World'
139
+ * }
140
+ * })
141
+ *
142
+ * // with http
143
+ * import { useHttp } from '@faasjs/http'
144
+ *
145
+ * export default useFunc(() => {
146
+ * const http = useHttp<{ name: string }>()
147
+ *
148
+ * return () => {
149
+ * return `Hello ${http.params.name}`
150
+ * }
151
+ * })
152
+ * ```
153
+ */
154
+ declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
155
+
156
+ export { Config, DeployData, ExportedHandler, Func, FuncConfig, Handler, InvokeData, LifeCycleKey, MountData, Next, Plugin, ProviderConfig, UseifyPlugin, useFunc, usePlugin };
package/dist/index.js CHANGED
@@ -60,6 +60,12 @@ var RunHandler = class {
60
60
  // src/index.ts
61
61
  var import_crypto = require("crypto");
62
62
  var Func = class {
63
+ /**
64
+ * Create a cloud function
65
+ * @param config {object} config
66
+ * @param config.plugins {Plugin[]} plugins list
67
+ * @param config.handler {Handler} business logic
68
+ */
63
69
  constructor(config) {
64
70
  this.handler = config.handler;
65
71
  this.plugins = config.plugins || [];
@@ -120,6 +126,13 @@ var Func = class {
120
126
  return await dispatch(0);
121
127
  };
122
128
  }
129
+ /**
130
+ * Deploy the function
131
+ * @param data {object} data
132
+ * @param data.root {string} root path
133
+ * @param data.filename {string} filename
134
+ * @param data.env {string} environment
135
+ */
123
136
  deploy(data) {
124
137
  if (!data.logger)
125
138
  data.logger = new import_logger.Logger("Func");
@@ -127,6 +140,9 @@ var Func = class {
127
140
  data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
128
141
  return this.compose("onDeploy")(data);
129
142
  }
143
+ /**
144
+ * First time mount the function
145
+ */
130
146
  async mount(data) {
131
147
  if (!data.logger)
132
148
  data.logger = new import_logger.Logger("Func");
@@ -146,6 +162,10 @@ var Func = class {
146
162
  data.logger.timeEnd("mount", "mounted");
147
163
  }
148
164
  }
165
+ /**
166
+ * Invoke the function
167
+ * @param data {object} data
168
+ */
149
169
  async invoke(data) {
150
170
  if (!this.mounted)
151
171
  await this.mount({
@@ -161,6 +181,9 @@ var Func = class {
161
181
  data.response = error;
162
182
  }
163
183
  }
184
+ /**
185
+ * Export the function
186
+ */
164
187
  export() {
165
188
  const handler = async (event, context, callback) => {
166
189
  if (typeof context === "undefined")
@@ -168,7 +191,7 @@ var Func = class {
168
191
  if (!context.request_id)
169
192
  context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
170
193
  if (!context.request_at)
171
- context.request_at = Math.round(new Date().getTime() / 1e3);
194
+ context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
172
195
  context.callbackWaitsForEmptyEventLoop = false;
173
196
  const logger = new import_logger.Logger(context.request_id);
174
197
  logger.debug("event: %j", event);
package/dist/index.mjs CHANGED
@@ -34,6 +34,12 @@ var RunHandler = class {
34
34
  // src/index.ts
35
35
  import { randomBytes } from "crypto";
36
36
  var Func = class {
37
+ /**
38
+ * Create a cloud function
39
+ * @param config {object} config
40
+ * @param config.plugins {Plugin[]} plugins list
41
+ * @param config.handler {Handler} business logic
42
+ */
37
43
  constructor(config) {
38
44
  this.handler = config.handler;
39
45
  this.plugins = config.plugins || [];
@@ -94,6 +100,13 @@ var Func = class {
94
100
  return await dispatch(0);
95
101
  };
96
102
  }
103
+ /**
104
+ * Deploy the function
105
+ * @param data {object} data
106
+ * @param data.root {string} root path
107
+ * @param data.filename {string} filename
108
+ * @param data.env {string} environment
109
+ */
97
110
  deploy(data) {
98
111
  if (!data.logger)
99
112
  data.logger = new Logger("Func");
@@ -101,6 +114,9 @@ var Func = class {
101
114
  data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
102
115
  return this.compose("onDeploy")(data);
103
116
  }
117
+ /**
118
+ * First time mount the function
119
+ */
104
120
  async mount(data) {
105
121
  if (!data.logger)
106
122
  data.logger = new Logger("Func");
@@ -120,6 +136,10 @@ var Func = class {
120
136
  data.logger.timeEnd("mount", "mounted");
121
137
  }
122
138
  }
139
+ /**
140
+ * Invoke the function
141
+ * @param data {object} data
142
+ */
123
143
  async invoke(data) {
124
144
  if (!this.mounted)
125
145
  await this.mount({
@@ -135,6 +155,9 @@ var Func = class {
135
155
  data.response = error;
136
156
  }
137
157
  }
158
+ /**
159
+ * Export the function
160
+ */
138
161
  export() {
139
162
  const handler = async (event, context, callback) => {
140
163
  if (typeof context === "undefined")
@@ -142,7 +165,7 @@ var Func = class {
142
165
  if (!context.request_id)
143
166
  context.request_id = randomBytes(16).toString("hex");
144
167
  if (!context.request_at)
145
- context.request_at = Math.round(new Date().getTime() / 1e3);
168
+ context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
146
169
  context.callbackWaitsForEmptyEventLoop = false;
147
170
  const logger = new Logger(context.request_id);
148
171
  logger.debug("event: %j", event);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/func",
3
- "version": "0.0.3-beta.86",
3
+ "version": "0.0.3-beta.87",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,14 +15,14 @@
15
15
  },
16
16
  "funding": "https://github.com/sponsors/faasjs",
17
17
  "scripts": {
18
- "build": "tsup-node src/index.ts --format esm,cjs --dts --clean"
18
+ "build": "tsup-node src/index.ts --config ../../tsup.config.json"
19
19
  },
20
20
  "files": [
21
21
  "dist"
22
22
  ],
23
23
  "dependencies": {
24
- "@faasjs/deep_merge": "^0.0.3-beta.86",
25
- "@faasjs/logger": "^0.0.3-beta.86"
24
+ "@faasjs/deep_merge": "^0.0.3-beta.87",
25
+ "@faasjs/logger": "^0.0.3-beta.87"
26
26
  },
27
27
  "engines": {
28
28
  "npm": ">=8.0.0",