cloud-function-cli 1.1.3 → 1.1.4

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.
@@ -8,6 +8,7 @@ const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const logger_1 = require("../utils/logger");
10
10
  const api_template_1 = require("../template/api-template");
11
+ const context_types_1 = require("../template/context-types");
11
12
  const create = async (name) => {
12
13
  const parts = name.split('/');
13
14
  let group, api;
@@ -20,6 +21,14 @@ const create = async (name) => {
20
21
  api = parts[1];
21
22
  }
22
23
  const apiRootDir = path_1.default.join(process.cwd(), 'api');
24
+ if (!fs_1.default.existsSync(apiRootDir)) {
25
+ fs_1.default.mkdirSync(apiRootDir, { recursive: true });
26
+ }
27
+ const typesPath = path_1.default.join(apiRootDir, '_cf.d.ts');
28
+ if (!fs_1.default.existsSync(typesPath)) {
29
+ fs_1.default.writeFileSync(typesPath, context_types_1.contextTypesDts);
30
+ logger_1.logger.success('Created context types: api/_cf.d.ts');
31
+ }
23
32
  const groupDir = path_1.default.join(apiRootDir, group);
24
33
  if (!fs_1.default.existsSync(groupDir)) {
25
34
  fs_1.default.mkdirSync(groupDir, { recursive: true });
@@ -1,13 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.apiTemplate = void 0;
4
- exports.apiTemplate = `async function handler(context) {
5
- const { request, utils } = context
4
+ exports.apiTemplate = `
5
+ /** @typedef {import('../_cf').CloudFunctionContext} CloudFunctionContext */
6
+
7
+ /**
8
+ * @param {CloudFunctionContext} context
9
+ */
10
+ async function handler(context) {
11
+ const { request, libs, packages, files, mongo, cacher, log } = context
6
12
 
7
13
  const query = request.query
8
14
  const data = request.body
9
- const uploadResult = await utils.files.handleUploadToGridFS(context, { path: 'uploads/', take: 1 })
10
- if (uploadResult) return uploadResult
11
15
 
12
16
  return { code: 1, data: data || query }
13
17
  }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contextTypesDts = void 0;
4
+ exports.contextTypesDts = `export type RuntimeEnvironment = 'dev' | 'prod'
5
+
6
+ export type UploadedFile = {
7
+ fieldname?: string
8
+ originalname?: string
9
+ encoding?: string
10
+ mimetype?: string
11
+ size?: number
12
+ buffer?: any
13
+ }
14
+
15
+ export type CloudFunctionRequest = {
16
+ body: any
17
+ query: any
18
+ params?: any
19
+ headers?: Record<string, any>
20
+ method?: string
21
+ path?: string
22
+ originalUrl?: string
23
+ url?: string
24
+ files?: UploadedFile[]
25
+ }
26
+
27
+ export type CloudFunctionResponse = {
28
+ status?: (code: number) => any
29
+ json?: (body: any) => any
30
+ send?: (body: any) => any
31
+ setHeader?: (name: string, value: any) => any
32
+ end?: () => any
33
+ headersSent?: boolean
34
+ }
35
+
36
+ export type FileService = {
37
+ uploadBuffer: (input: { buffer: any; filename: string; contentType?: string; metadata?: Record<string, any> }) => Promise<string>
38
+ findById: (fileId: string) => Promise<any | null>
39
+ openDownloadStream: (fileId: string) => any
40
+ downloadBuffer: (fileId: string) => Promise<any>
41
+ delete: (fileId: string) => Promise<void>
42
+ listFiles: (opts?: { page?: number; limit?: number; pathPrefix?: string }) => Promise<{ items: any[]; page: number; limit: number; total: number }>
43
+ }
44
+
45
+ export type LibLoader = {
46
+ use: (name: string) => Promise<any>
47
+ }
48
+
49
+ export type Packages = {
50
+ ensure: (spec: string) => Promise<{ name: string; spec: string }>
51
+ import: (nameOrSpec: string) => any
52
+ importOrInstall: (spec: string) => Promise<any>
53
+ }
54
+
55
+ export type CloudFunctionContext = {
56
+ log: {
57
+ info: (...args: any[]) => void
58
+ warn: (...args: any[]) => void
59
+ error: (...args: any[]) => void
60
+ debug: (...args: any[]) => void
61
+ }
62
+ cacher: {
63
+ get: (key: string) => Promise<any>
64
+ set: (key: string, value: any, ttlSeconds?: number) => Promise<void>
65
+ del: (key: string) => Promise<void>
66
+ }
67
+ options: {
68
+ group: string
69
+ apiName: string
70
+ option?: string
71
+ environment?: RuntimeEnvironment
72
+ }
73
+ environment?: RuntimeEnvironment
74
+ mongo: {
75
+ getDB: (dbName?: string) => any
76
+ }
77
+ files: FileService
78
+ libs: LibLoader
79
+ packages: Packages
80
+ http: any
81
+ https: any
82
+ request: CloudFunctionRequest
83
+ response: CloudFunctionResponse
84
+ }
85
+ `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloud-function-cli",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "CLI for Cloud Function System - Create, deploy, and manage serverless functions",
5
5
  "main": "dist/index.js",
6
6
  "bin": {