|
| 1 | +import { AxiosStatic } from 'axios' |
| 2 | +import * as mongodb from 'mongodb' |
| 3 | +import { Db } from 'database-proxy' |
| 4 | +import { WebSocket } from 'ws' |
| 5 | +import { FunctionContext } from './function.interface' |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +export type InvokeFunctionType = ( |
| 10 | + name: string, |
| 11 | + param: FunctionContext, |
| 12 | +) => Promise<any> |
| 13 | +export type GetTokenFunctionType = (payload: any, secret?: string) => string |
| 14 | +export type ParseTokenFunctionType = ( |
| 15 | + token: string, |
| 16 | + secret?: string, |
| 17 | +) => any | null |
| 18 | + |
| 19 | +export interface MongoDriverObject { |
| 20 | + client: mongodb.MongoClient |
| 21 | + db: mongodb.Db |
| 22 | +} |
| 23 | + |
| 24 | +export interface CloudSdkInterface { |
| 25 | + /** |
| 26 | + * Sending an HTTP request is actually an Axios instance. You can refer to the Axios documentation directly |
| 27 | + */ |
| 28 | + fetch: AxiosStatic |
| 29 | + |
| 30 | + /** |
| 31 | + * Get a database-ql instance |
| 32 | + */ |
| 33 | + database(): Db |
| 34 | + |
| 35 | + /** |
| 36 | + * Invoke cloud function |
| 37 | + */ |
| 38 | + invoke: InvokeFunctionType |
| 39 | + |
| 40 | + /** |
| 41 | + * Cloud function global memory `shared` object, which can share data across multiple requests and different cloud functions |
| 42 | + * 1. Some global configurations can be initialized into `shared`, such as 3rd-party API configuration |
| 43 | + * 2. You can share some common methods, such as checkPermission(), to improve the performance of cloud functions |
| 44 | + * 3. It can cache hot data and is recommended to use it in a small amount (this object is allocated in the node VM heap because of the memory limit of the node VM heap) |
| 45 | + */ |
| 46 | + shared: Map<string, any> |
| 47 | + |
| 48 | + /** |
| 49 | + * Generate a JWT Token, if don't provide `secret` fields, use current server secret key to do signature |
| 50 | + */ |
| 51 | + getToken: GetTokenFunctionType |
| 52 | + |
| 53 | + /** |
| 54 | + * Parse a JWT Token, if don't provide `secret` fields, use current server secret key to verify signature |
| 55 | + */ |
| 56 | + parseToken: ParseTokenFunctionType |
| 57 | + |
| 58 | + /** |
| 59 | + * The mongodb instance of MongoDB node.js native driver. |
| 60 | + * |
| 61 | + * Because the laf.js database-QL has only partial data manipulation capability, |
| 62 | + * expose this `mongo` object to the cloud function, so that the cloud function has full database manipulation capability: |
| 63 | + * 1. Transaction operations |
| 64 | + * ```js |
| 65 | + * const session = mongo.client.startSession() |
| 66 | + * try { |
| 67 | + * await session.withTransaction(async () => { |
| 68 | + * await mongo.db.collection('xxx').updateOne({}, { session }) |
| 69 | + * await mongo.db.collection('yyy').deleteMany({}, { session }) |
| 70 | + * }) |
| 71 | + * } finally { |
| 72 | + * await session.endSession() |
| 73 | + * } |
| 74 | + * ``` |
| 75 | + * 2. Indexes operations |
| 76 | + * ```js |
| 77 | + * await mongo.db.collection('users').createIndex('username', { unique: true }) |
| 78 | + * ``` |
| 79 | + * 3. Aggregation operations |
| 80 | + * ```js |
| 81 | + * await mongo.db.collection('users') |
| 82 | + * .aggregate([]) |
| 83 | + * .toArray() |
| 84 | + * ``` |
| 85 | + */ |
| 86 | + mongo: MongoDriverObject |
| 87 | + |
| 88 | + /** |
| 89 | + * Websocket connection list |
| 90 | + */ |
| 91 | + sockets: Set<WebSocket> |
| 92 | + |
| 93 | + /** |
| 94 | + * Current app id |
| 95 | + */ |
| 96 | + appid: string |
| 97 | + |
| 98 | + env: { |
| 99 | + DB_URI?: string |
| 100 | + SERVER_SECRET?: string |
| 101 | + APP_ID?: string |
| 102 | + OSS_ACCESS_KEY?: string |
| 103 | + OSS_ACCESS_SECRET?: string |
| 104 | + OSS_REGION?: string |
| 105 | + OSS_INTERNAL_ENDPOINT?: string |
| 106 | + OSS_EXTERNAL_ENDPOINT?: string |
| 107 | + NPM_INSTALL_FLAGS?: string |
| 108 | + RUNTIME_IMAGE?: string |
| 109 | + } |
| 110 | +} |
0 commit comments