forked from capawesome-team/capacitor-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.ts
More file actions
55 lines (51 loc) · 1.49 KB
/
Copy pathweb.ts
File metadata and controls
55 lines (51 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { WebPlugin } from '@capacitor/core';
import {
connectFunctionsEmulator,
getFunctions,
httpsCallable,
httpsCallableFromURL,
} from 'firebase/functions';
import type {
CallByNameOptions,
CallByNameResult,
CallByUrlOptions,
CallResult,
FirebaseFunctionsPlugin,
UseEmulatorOptions,
} from './definitions';
export class FirebaseFunctionsWeb
extends WebPlugin
implements FirebaseFunctionsPlugin
{
public async callByName<RequestData = unknown, ResponseData = unknown>(
options: CallByNameOptions<RequestData>,
): Promise<CallByNameResult<ResponseData>> {
const functions = getFunctions(undefined, options.region);
const callable = httpsCallable<RequestData, ResponseData>(
functions,
options.name,
);
const result = await callable(options.data);
return {
data: result.data,
};
}
public async callByUrl<RequestData = unknown, ResponseData = unknown>(
options: CallByUrlOptions<RequestData>,
): Promise<CallResult<ResponseData>> {
const functions = getFunctions();
const callable = httpsCallableFromURL<RequestData, ResponseData>(
functions,
options.url,
);
const result = await callable(options.data);
return {
data: result.data,
};
}
public async useEmulator(options: UseEmulatorOptions): Promise<void> {
const functions = getFunctions(undefined, options.regionOrCustomDomain);
const port = options.port || 5001;
connectFunctionsEmulator(functions, options.host, port);
}
}