@ienlab/cloud-functions-library 1.0.0-dev.15 → 1.0.0-dev.16
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,44 @@
|
|
|
1
|
+
import { CallableOptions, CallableRequest } from 'firebase-functions/v2/https';
|
|
2
|
+
export type PrimitiveMap = {
|
|
3
|
+
string: string;
|
|
4
|
+
number: number;
|
|
5
|
+
boolean: boolean;
|
|
6
|
+
null: null;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* "string" | "number | null" 같은 문자열을 실제 타입으로 변환
|
|
10
|
+
*/
|
|
11
|
+
export type ParseType<T extends string> = T extends keyof PrimitiveMap ? PrimitiveMap[T] : T extends `${infer A} | ${infer B}` ? ParseType<A> | ParseType<B> : never;
|
|
12
|
+
export type SchemaRecord = {
|
|
13
|
+
/** 실제 Cloud Function 이름 (배포되는 이름) */
|
|
14
|
+
name: string;
|
|
15
|
+
/** params 각 필드의 타입 정의 ("string", "number | null" 등) */
|
|
16
|
+
params: Record<string, string>;
|
|
17
|
+
/** result 각 필드의 타입 정의 */
|
|
18
|
+
result: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
export type FnSchemaLike = Record<string, SchemaRecord>;
|
|
21
|
+
export type FnKey<TSchema extends FnSchemaLike> = keyof TSchema;
|
|
22
|
+
export type FnCallableName<TSchema extends FnSchemaLike, K extends FnKey<TSchema>> = TSchema[K]["name"];
|
|
23
|
+
/** "string" -> string, "number | null" -> number | null 로 바꿔줌 */
|
|
24
|
+
export type ToRuntimeType<T extends Record<string, string>> = {
|
|
25
|
+
[K in keyof T]: ParseType<T[K]>;
|
|
26
|
+
};
|
|
27
|
+
/** 스키마에서 params 타입 뽑기 */
|
|
28
|
+
export type FnParams<TSchema extends FnSchemaLike, K extends FnKey<TSchema>> = ToRuntimeType<TSchema[K]["params"]>;
|
|
29
|
+
/** 스키마에서 result 타입 뽑기 */
|
|
30
|
+
export type FnResult<TSchema extends FnSchemaLike, K extends FnKey<TSchema>> = ToRuntimeType<TSchema[K]["result"]>;
|
|
31
|
+
/**
|
|
32
|
+
* 서버에서 callable 함수 정의할 때 쓰는 헬퍼
|
|
33
|
+
*
|
|
34
|
+
* 사용 예)
|
|
35
|
+
* export const getUserProfile = defineCallable(
|
|
36
|
+
* fnSchema,
|
|
37
|
+
* "getUserProfile",
|
|
38
|
+
* async (data, req) => {
|
|
39
|
+
* // data: FnParams<typeof fnSchema, "getUserProfile">
|
|
40
|
+
* // return: FnResult<typeof fnSchema, "getUserProfile">
|
|
41
|
+
* },
|
|
42
|
+
* )
|
|
43
|
+
*/
|
|
44
|
+
export declare function defineCallable<TSchema extends FnSchemaLike, K extends FnKey<TSchema>>(_schema: TSchema, _key: K, handler: (data: FnParams<TSchema, K>, req: CallableRequest<FnParams<TSchema, K>>) => FnResult<TSchema, K> | Promise<FnResult<TSchema, K>>, options?: CallableOptions<FnParams<TSchema, K>>): import('firebase-functions/v2/https').CallableFunction<ToRuntimeType<TSchema[K]["params"]>, any, unknown>;
|