fauna-api 0.0.0-stage → 1.0.676

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.
Files changed (171) hide show
  1. package/dist/brands.d.ts +2 -0
  2. package/dist/brands.js +2 -0
  3. package/dist/index.d.ts +64 -0
  4. package/dist/index.js +121 -0
  5. package/dist/src/ant/chat.d.ts +130 -0
  6. package/dist/src/ant/chat.js +467 -0
  7. package/dist/src/ant/format.d.ts +40 -0
  8. package/dist/src/ant/format.js +143 -0
  9. package/dist/src/ant/graph.d.ts +130 -0
  10. package/dist/src/ant/graph.js +449 -0
  11. package/dist/src/ant/index.d.ts +16 -0
  12. package/dist/src/ant/index.js +61 -0
  13. package/dist/src/ant/persona.d.ts +151 -0
  14. package/dist/src/ant/persona.js +136 -0
  15. package/dist/src/ant/timeline.d.ts +283 -0
  16. package/dist/src/ant/timeline.js +1025 -0
  17. package/dist/src/authentification.d.ts +45 -0
  18. package/dist/src/authentification.js +194 -0
  19. package/dist/src/billing.d.ts +135 -0
  20. package/dist/src/billing.js +128 -0
  21. package/dist/src/calendar.d.ts +83 -0
  22. package/dist/src/calendar.js +133 -0
  23. package/dist/src/chats.d.ts +113 -0
  24. package/dist/src/chats.js +100 -0
  25. package/dist/src/cicada/contacts.d.ts +31 -0
  26. package/dist/src/cicada/contacts.js +28 -0
  27. package/dist/src/cicada/conversations.d.ts +73 -0
  28. package/dist/src/cicada/conversations.js +236 -0
  29. package/dist/src/cicada/inboxes.d.ts +48 -0
  30. package/dist/src/cicada/inboxes.js +82 -0
  31. package/dist/src/cicada/index.d.ts +16 -0
  32. package/dist/src/cicada/index.js +61 -0
  33. package/dist/src/cicada/phone.d.ts +48 -0
  34. package/dist/src/cicada/phone.js +306 -0
  35. package/dist/src/drive.d.ts +80 -0
  36. package/dist/src/drive.js +472 -0
  37. package/dist/src/extensions/index.d.ts +4 -0
  38. package/dist/src/extensions/index.js +5 -0
  39. package/dist/src/extensions/web/index.d.ts +6 -0
  40. package/dist/src/extensions/web/index.js +7 -0
  41. package/dist/src/extensions/web/session.d.ts +106 -0
  42. package/dist/src/extensions/web/session.js +469 -0
  43. package/dist/src/extensions/web/tools.d.ts +13 -0
  44. package/dist/src/extensions/web/tools.js +72 -0
  45. package/dist/src/friends.d.ts +39 -0
  46. package/dist/src/friends.js +119 -0
  47. package/dist/src/groups.d.ts +82 -0
  48. package/dist/src/groups.js +269 -0
  49. package/dist/src/inbox.d.ts +11 -0
  50. package/dist/src/inbox.js +35 -0
  51. package/dist/src/integrations.d.ts +52 -0
  52. package/dist/src/integrations.js +205 -0
  53. package/dist/src/magpie/explorer.d.ts +14 -0
  54. package/dist/src/magpie/explorer.js +36 -0
  55. package/dist/src/magpie/index.d.ts +16 -0
  56. package/dist/src/magpie/index.js +61 -0
  57. package/dist/src/magpie/portfolio.d.ts +52 -0
  58. package/dist/src/magpie/portfolio.js +179 -0
  59. package/dist/src/magpie/quotes.d.ts +35 -0
  60. package/dist/src/magpie/quotes.js +47 -0
  61. package/dist/src/magpie/room.d.ts +61 -0
  62. package/dist/src/magpie/room.js +238 -0
  63. package/dist/src/maps.d.ts +159 -0
  64. package/dist/src/maps.js +171 -0
  65. package/dist/src/marmot/browser.d.ts +139 -0
  66. package/dist/src/marmot/browser.js +389 -0
  67. package/dist/src/marmot/index.d.ts +20 -0
  68. package/dist/src/marmot/index.js +65 -0
  69. package/dist/src/marmot/media.d.ts +56 -0
  70. package/dist/src/marmot/media.js +265 -0
  71. package/dist/src/marmot/research.d.ts +337 -0
  72. package/dist/src/marmot/research.js +1138 -0
  73. package/dist/src/marmot/scraper.d.ts +59 -0
  74. package/dist/src/marmot/scraper.js +94 -0
  75. package/dist/src/marmot/web.d.ts +402 -0
  76. package/dist/src/marmot/web.js +2586 -0
  77. package/dist/src/marmot/workspace.d.ts +81 -0
  78. package/dist/src/marmot/workspace.js +263 -0
  79. package/dist/src/parrot/agent.d.ts +53 -0
  80. package/dist/src/parrot/agent.js +93 -0
  81. package/dist/src/parrot/chat.d.ts +84 -0
  82. package/dist/src/parrot/chat.js +237 -0
  83. package/dist/src/parrot/format.d.ts +128 -0
  84. package/dist/src/parrot/format.js +639 -0
  85. package/dist/src/parrot/generative.d.ts +261 -0
  86. package/dist/src/parrot/generative.js +185 -0
  87. package/dist/src/parrot/index.d.ts +22 -0
  88. package/dist/src/parrot/index.js +67 -0
  89. package/dist/src/parrot/room.d.ts +225 -0
  90. package/dist/src/parrot/room.js +466 -0
  91. package/dist/src/parrot/routing.d.ts +90 -0
  92. package/dist/src/parrot/routing.js +78 -0
  93. package/dist/src/parrot/share.d.ts +126 -0
  94. package/dist/src/parrot/share.js +261 -0
  95. package/dist/src/parrot/socket.d.ts +41 -0
  96. package/dist/src/parrot/socket.js +478 -0
  97. package/dist/src/payments.d.ts +73 -0
  98. package/dist/src/payments.js +107 -0
  99. package/dist/src/private/cards.css +75 -0
  100. package/dist/src/rooms.d.ts +76 -0
  101. package/dist/src/rooms.js +168 -0
  102. package/dist/src/shared/schema.d.ts +24 -0
  103. package/dist/src/shared/schema.js +1 -0
  104. package/dist/src/shared/server.d.ts +65 -0
  105. package/dist/src/shared/server.js +302 -0
  106. package/dist/src/termite/environment.d.ts +110 -0
  107. package/dist/src/termite/environment.js +263 -0
  108. package/dist/src/termite/index.d.ts +14 -0
  109. package/dist/src/termite/index.js +59 -0
  110. package/dist/src/termite/os.d.ts +326 -0
  111. package/dist/src/termite/os.js +572 -0
  112. package/dist/src/termite/versioning.d.ts +94 -0
  113. package/dist/src/termite/versioning.js +217 -0
  114. package/dist/src/tickets.d.ts +43 -0
  115. package/dist/src/tickets.js +138 -0
  116. package/dist/src/travel.d.ts +85 -0
  117. package/dist/src/travel.js +44 -0
  118. package/dist/src/ui/authentification.d.ts +24 -0
  119. package/dist/src/ui/authentification.js +177 -0
  120. package/dist/src/ui/brands.css +11 -0
  121. package/dist/src/ui/brands.d.ts +7 -0
  122. package/dist/src/ui/brands.js +14 -0
  123. package/dist/src/ui/browser.d.ts +74 -0
  124. package/dist/src/ui/browser.js +470 -0
  125. package/dist/src/ui/chat.d.ts +183 -0
  126. package/dist/src/ui/chat.js +2379 -0
  127. package/dist/src/ui/chatPanel.d.ts +109 -0
  128. package/dist/src/ui/chatPanel.js +768 -0
  129. package/dist/src/ui/chatSequence.d.ts +36 -0
  130. package/dist/src/ui/chatSequence.js +42 -0
  131. package/dist/src/ui/crown.d.ts +19 -0
  132. package/dist/src/ui/crown.js +199 -0
  133. package/dist/src/ui/cursor.d.ts +73 -0
  134. package/dist/src/ui/cursor.js +175 -0
  135. package/dist/src/ui/device.d.ts +33 -0
  136. package/dist/src/ui/device.js +49 -0
  137. package/dist/src/ui/drive.d.ts +69 -0
  138. package/dist/src/ui/drive.js +1243 -0
  139. package/dist/src/ui/generative.d.ts +39 -0
  140. package/dist/src/ui/generative.js +313 -0
  141. package/dist/src/ui/hover.d.ts +6 -0
  142. package/dist/src/ui/hover.js +210 -0
  143. package/dist/src/ui/index.d.ts +42 -0
  144. package/dist/src/ui/index.js +87 -0
  145. package/dist/src/ui/integrations.d.ts +13 -0
  146. package/dist/src/ui/integrations.js +175 -0
  147. package/dist/src/ui/maps.d.ts +87 -0
  148. package/dist/src/ui/maps.js +547 -0
  149. package/dist/src/ui/roster.d.ts +32 -0
  150. package/dist/src/ui/roster.js +163 -0
  151. package/dist/src/ui/talk.d.ts +12 -0
  152. package/dist/src/ui/talk.js +560 -0
  153. package/dist/src/ui/tickets.d.ts +13 -0
  154. package/dist/src/ui/tickets.js +966 -0
  155. package/dist/src/ui/toast.d.ts +7 -0
  156. package/dist/src/ui/toast.js +258 -0
  157. package/dist/src/ui/tokenText.d.ts +18 -0
  158. package/dist/src/ui/tokenText.js +96 -0
  159. package/dist/src/ui/tokenized.d.ts +42 -0
  160. package/dist/src/ui/tokenized.js +622 -0
  161. package/dist/src/ui/ui.css +4763 -0
  162. package/dist/src/ui/view.d.ts +23 -0
  163. package/dist/src/ui/view.js +584 -0
  164. package/dist/src/view.d.ts +113 -0
  165. package/dist/src/view.js +386 -0
  166. package/dist/src/webhooks.d.ts +66 -0
  167. package/dist/src/webhooks.js +78 -0
  168. package/dist/ui.d.ts +1 -0
  169. package/dist/ui.js +1 -0
  170. package/package.json +89 -6
  171. package/README.md +0 -3
@@ -0,0 +1,45 @@
1
+ import { Fauna } from "../index.js";
2
+ import { Platform } from "./integrations.js";
3
+ import type { Group } from "./groups.js";
4
+ export interface Credentials {
5
+ token: Token;
6
+ emailVerified: boolean;
7
+ phoneVerified: boolean;
8
+ permissions: string[];
9
+ integrations: Platform[];
10
+ email: string;
11
+ phone: string;
12
+ displayName?: string;
13
+ icon?: string;
14
+ apiKeys: ApiKey[];
15
+ balance: number;
16
+ }
17
+ export type ApiKey = {
18
+ id: string;
19
+ balance: number;
20
+ services: string[];
21
+ name?: string;
22
+ };
23
+ export type Token = string;
24
+ export type Profile = {
25
+ email: string;
26
+ displayName?: string;
27
+ icon?: string;
28
+ };
29
+ export declare const Profile: {
30
+ fromJSON(json: unknown): Profile | undefined;
31
+ };
32
+ export declare function authorize(email: string, password: string): Promise<Token>;
33
+ export declare function authorizeToken(token: Token): Promise<Credentials>;
34
+ export declare function authorizeApiKey(apiKey: string): Promise<ApiKey>;
35
+ export declare function create(email: string, password: string, phone: string, displayName?: string): Promise<boolean>;
36
+ /** email and phone are only accepted while that one isn't verified yet; a change sends a new link and code. */
37
+ export declare function edit(credentials: Credentials, update: Partial<Pick<Credentials, 'displayName' | 'icon' | 'email' | 'phone'>>): Promise<boolean>;
38
+ export declare function verify(channel: 'EMAIL' | 'PHONE', target: string, code: string): Promise<boolean>;
39
+ export declare function sendVerification(credentials: Credentials, channel: 'EMAIL' | 'PHONE'): Promise<boolean>;
40
+ export declare function allocateBalance(credentials: Credentials, target: ApiKey | Group, delta: number): Promise<true>;
41
+ export declare function generateApiKey(credentials: Credentials): Promise<ApiKey>;
42
+ export declare function removeApiKey(credentials: Credentials, apiKey: ApiKey): Promise<boolean>;
43
+ export declare function profile(credentials: Credentials, email: string): Promise<Profile>;
44
+ export declare function query(credentials: Credentials, profile: Partial<Profile>): Promise<Profile[]>;
45
+ export declare function editApiKey(credentials: Credentials, apiKey: ApiKey, update: Partial<ApiKey>): Promise<boolean>;
@@ -0,0 +1,194 @@
1
+ import { Fauna } from "../index.js";
2
+ export const Profile = {
3
+ fromJSON(json) {
4
+ if (!json || typeof json !== "object")
5
+ return undefined;
6
+ const row = json;
7
+ if (typeof row.email !== "string" || !row.email)
8
+ return undefined;
9
+ return {
10
+ email: row.email,
11
+ displayName: typeof row.displayName === "string" ? row.displayName : undefined,
12
+ icon: typeof row.icon === "string" ? row.icon : undefined,
13
+ };
14
+ },
15
+ };
16
+ // Authorization
17
+ export async function authorize(email, password) {
18
+ const response = await fetch(`${Fauna.getURL()}/authentification/authorize`, {
19
+ method: "POST",
20
+ headers: {
21
+ "Content-Type": "application/json"
22
+ },
23
+ body: JSON.stringify({ email: email, password: password })
24
+ });
25
+ if (!response.ok) {
26
+ throw new Error(await response.text());
27
+ }
28
+ return await response.text();
29
+ }
30
+ export async function authorizeToken(token) {
31
+ const response = await fetch(`${Fauna.getURL()}/authentification/authorize`, {
32
+ method: "POST",
33
+ headers: {
34
+ "Authorization": `Token ${token}`
35
+ }
36
+ });
37
+ if (!response.ok) {
38
+ throw new Error(await response.text());
39
+ }
40
+ const credentials = await response.json();
41
+ return { ...credentials, token: String(token) };
42
+ }
43
+ export async function authorizeApiKey(apiKey) {
44
+ const response = await fetch(`${Fauna.getURL()}/authentification/authorize`, {
45
+ method: "POST",
46
+ headers: {
47
+ "Authorization": `ApiKey ${apiKey}`
48
+ }
49
+ });
50
+ if (!response.ok) {
51
+ throw new Error(await response.text());
52
+ }
53
+ return await response.json();
54
+ }
55
+ // Credentials Management
56
+ export async function create(email, password, phone, displayName) {
57
+ const response = await fetch(`${Fauna.getURL()}/authentification/create`, {
58
+ method: "POST",
59
+ headers: {
60
+ "Content-Type": "application/json"
61
+ },
62
+ body: JSON.stringify({ email, password, phone, displayName })
63
+ });
64
+ if (!response.ok) {
65
+ throw new Error(await response.text());
66
+ }
67
+ return response.ok;
68
+ }
69
+ /** email and phone are only accepted while that one isn't verified yet; a change sends a new link and code. */
70
+ export async function edit(credentials, update) {
71
+ const response = await fetch(`${Fauna.getURL()}/authentification`, {
72
+ method: "PATCH",
73
+ headers: {
74
+ "Content-Type": "application/json",
75
+ "Authorization": `Token ${credentials.token}`
76
+ },
77
+ body: JSON.stringify(update)
78
+ });
79
+ if (!response.ok) {
80
+ throw new Error(await response.text());
81
+ }
82
+ return response.ok;
83
+ }
84
+ export async function verify(channel, target, code) {
85
+ const response = await fetch(`${Fauna.getURL()}/authentification/verify`, {
86
+ method: "POST",
87
+ headers: {
88
+ "Content-Type": "application/json"
89
+ },
90
+ body: JSON.stringify({ channel, target, code })
91
+ });
92
+ if (!response.ok) {
93
+ throw new Error(await response.text());
94
+ }
95
+ return response.ok;
96
+ }
97
+ export async function sendVerification(credentials, channel) {
98
+ const response = await fetch(`${Fauna.getURL()}/authentification/send-verification`, {
99
+ method: "POST",
100
+ headers: {
101
+ "Content-Type": "application/json",
102
+ "Authorization": `Token ${credentials.token}`
103
+ },
104
+ body: JSON.stringify({ channel })
105
+ });
106
+ if (!response.ok) {
107
+ throw new Error(await response.text());
108
+ }
109
+ return response.ok;
110
+ }
111
+ export async function allocateBalance(credentials, target, delta) {
112
+ const deltaMicros = Math.round(delta * 1000000);
113
+ if (!Number.isSafeInteger(deltaMicros))
114
+ throw new Error('Invalid balance delta.');
115
+ const headers = {
116
+ "Content-Type": "application/json",
117
+ "Authorization": `Token ${credentials.token}`
118
+ };
119
+ headers['services' in target ? "Api-Key" : "Group"] = target.id;
120
+ const response = await fetch(`${Fauna.getURL()}/authentification/allocate-balance`, {
121
+ method: "POST",
122
+ headers,
123
+ body: JSON.stringify({ deltaMicros })
124
+ });
125
+ if (!response.ok) {
126
+ throw new Error(await response.text());
127
+ }
128
+ return response.ok;
129
+ }
130
+ export async function generateApiKey(credentials) {
131
+ const response = await fetch(`${Fauna.getURL()}/authentification/api-keys`, {
132
+ method: "POST",
133
+ headers: {
134
+ "Content-Type": "application/json",
135
+ "Authorization": `Token ${credentials.token}`
136
+ }
137
+ });
138
+ if (!response.ok) {
139
+ throw new Error(await response.text());
140
+ }
141
+ return await response.json();
142
+ }
143
+ export async function removeApiKey(credentials, apiKey) {
144
+ const response = await fetch(`${Fauna.getURL()}/authentification/api-keys/${encodeURIComponent(apiKey.id)}`, {
145
+ method: "DELETE",
146
+ headers: {
147
+ "Authorization": `Token ${credentials.token}`
148
+ }
149
+ });
150
+ if (!response.ok) {
151
+ throw new Error(await response.text());
152
+ }
153
+ return response.ok;
154
+ }
155
+ export async function profile(credentials, email) {
156
+ const response = await fetch(`${Fauna.getURL()}/authentification/profile/${encodeURIComponent(email)}`, {
157
+ method: "GET",
158
+ headers: {
159
+ "Authorization": `Token ${credentials.token}`
160
+ }
161
+ });
162
+ if (!response.ok) {
163
+ throw new Error(await response.text());
164
+ }
165
+ return await response.json();
166
+ }
167
+ export async function query(credentials, profile) {
168
+ const response = await fetch(`${Fauna.getURL()}/authentification/query`, {
169
+ method: "POST",
170
+ headers: {
171
+ "Content-Type": "application/json",
172
+ "Authorization": `Token ${credentials.token}`
173
+ },
174
+ body: JSON.stringify({ email: profile.email, displayName: profile.displayName })
175
+ });
176
+ if (!response.ok) {
177
+ throw new Error(await response.text());
178
+ }
179
+ return await response.json();
180
+ }
181
+ export async function editApiKey(credentials, apiKey, update) {
182
+ const response = await fetch(`${Fauna.getURL()}/authentification/api-keys/${encodeURIComponent(apiKey.id)}`, {
183
+ method: "PATCH",
184
+ headers: {
185
+ "Content-Type": "application/json",
186
+ "Authorization": `Token ${credentials.token}`
187
+ },
188
+ body: JSON.stringify({ name: update.name })
189
+ });
190
+ if (!response.ok) {
191
+ throw new Error(await response.text());
192
+ }
193
+ return response.ok;
194
+ }
@@ -0,0 +1,135 @@
1
+ import { Fauna } from "../index.js";
2
+ import { Token } from "./authentification.js";
3
+ export type PaymentMethod = {
4
+ brand: string;
5
+ last4: string;
6
+ expMonth: number;
7
+ expYear: number;
8
+ };
9
+ export type AutoReload = {
10
+ enabled: boolean;
11
+ threshold: number;
12
+ target: number;
13
+ monthlyCap: number;
14
+ monthlySpent: number;
15
+ status: string;
16
+ available: boolean;
17
+ };
18
+ export type AutoReloadUpdate = {
19
+ enabled: boolean;
20
+ threshold: number;
21
+ target: number;
22
+ monthlyCap: number;
23
+ };
24
+ export type Transaction = {
25
+ id: string;
26
+ kind: 'manual' | 'automatic' | 'subscription' | 'grant';
27
+ amount: number;
28
+ status: string;
29
+ failureMessage?: string;
30
+ createdAt: string;
31
+ completedAt?: string;
32
+ };
33
+ export type Plan = 'free' | 'premium' | 'enterprise';
34
+ export type Subscription = {
35
+ plan: Plan;
36
+ status: string;
37
+ periodEnd?: string;
38
+ cancelAtPeriodEnd: boolean;
39
+ /** Ids of the add-ons you hold. */
40
+ addons: string[];
41
+ };
42
+ /** Something an app sells on top of Premium, on the same subscription. Prices are in dollars per `interval`. */
43
+ export type Addon = {
44
+ id: string;
45
+ name: string;
46
+ price: number;
47
+ /** What it costs without Premium yet, since buying it adds Premium too. */
48
+ withPremium: number;
49
+ currency: string;
50
+ interval: string;
51
+ };
52
+ export type Summary = {
53
+ balance: number;
54
+ currency: 'usd';
55
+ paymentMethod?: PaymentMethod;
56
+ autoReload: AutoReload;
57
+ subscription: Subscription;
58
+ recentTransactions: Transaction[];
59
+ };
60
+ type WireAutoReload = {
61
+ enabled: boolean;
62
+ thresholdMicros: number;
63
+ targetMicros: number;
64
+ monthlyCapCents: number;
65
+ monthlySpentCents: number;
66
+ status: string;
67
+ available: boolean;
68
+ };
69
+ type WireTransaction = Omit<Transaction, 'amount'> & {
70
+ amountCents: number;
71
+ creditMicros: number;
72
+ };
73
+ export declare const AutoReload: {
74
+ fromJSON(json: WireAutoReload): AutoReload;
75
+ };
76
+ export declare const Transaction: {
77
+ fromJSON(json: WireTransaction): Transaction;
78
+ };
79
+ export declare function get(token: Token): Promise<Summary>;
80
+ /** Start a Stripe Checkout top-up for the caller's personal wallet. Stripe
81
+ * returns to `returnUrl` when it is a trusted app origin, else the Fauna app. */
82
+ export declare function checkout(token: Token, amount: number, options?: {
83
+ returnUrl?: string;
84
+ }): Promise<{
85
+ url: string;
86
+ }>;
87
+ export declare function setupPaymentMethod(token: Token, options?: {
88
+ returnUrl?: string;
89
+ }): Promise<{
90
+ url: string;
91
+ }>;
92
+ export declare function updateAutoReload(token: Token, update: AutoReloadUpdate): Promise<AutoReload>;
93
+ export declare function openPortal(token: Token, options?: {
94
+ returnUrl?: string;
95
+ }): Promise<{
96
+ url: string;
97
+ }>;
98
+ /**
99
+ * Subscribe to Premium, or with `addon`, buy an add-on. With a subscription
100
+ * already, the add-on joins it at once (the URL then returns to `returnUrl`, or
101
+ * opens the invoice when the card needs confirming); otherwise Checkout sells
102
+ * Premium and the add-on together. Open the returned URL.
103
+ */
104
+ export declare function subscribe(token: Token, options?: {
105
+ returnUrl?: string;
106
+ addon?: string;
107
+ }): Promise<{
108
+ url: string;
109
+ }>;
110
+ /** End Premium at the period's end (add-ons end with it), or with `addon`, stop that add-on now with a prorated credit. */
111
+ export declare function cancelSubscription(token: Token, options?: {
112
+ addon?: string;
113
+ }): Promise<Subscription>;
114
+ /** The add-ons for sale. */
115
+ export declare function addons(token: Token): Promise<Addon[]>;
116
+ export declare function transactions(token: Token, query: {
117
+ cursor: number;
118
+ max: number;
119
+ }): Promise<Transaction[]>;
120
+ export type UsagePoint = {
121
+ day: string;
122
+ service: string;
123
+ base: number;
124
+ spread: number;
125
+ };
126
+ export type Usage = {
127
+ principalKind: 'user' | 'group';
128
+ since: string;
129
+ usage: UsagePoint[];
130
+ };
131
+ /** Daily base/spread usage for the caller's wallet, or a group's (member-only).
132
+ * `days` bounds the window server-side (default 30, max 180). */
133
+ export declare function usage(token: Token, groupId?: string, days?: number): Promise<Usage>;
134
+ export declare function format(amount: number): string;
135
+ export {};
@@ -0,0 +1,128 @@
1
+ import { Fauna } from "../index.js";
2
+ async function request(token, path, init) {
3
+ const response = await fetch(`${Fauna.getURL()}${path}`, {
4
+ ...init,
5
+ headers: {
6
+ ...init?.headers,
7
+ "Authorization": `Token ${token}`,
8
+ },
9
+ });
10
+ if (!response.ok)
11
+ throw new Error(await response.text());
12
+ return await response.json();
13
+ }
14
+ export const AutoReload = {
15
+ fromJSON(json) {
16
+ return {
17
+ enabled: json.enabled,
18
+ threshold: json.thresholdMicros / 1000000,
19
+ target: json.targetMicros / 1000000,
20
+ monthlyCap: json.monthlyCapCents / 100,
21
+ monthlySpent: json.monthlySpentCents / 100,
22
+ status: json.status,
23
+ available: json.available === true,
24
+ };
25
+ },
26
+ };
27
+ export const Transaction = {
28
+ fromJSON(json) {
29
+ const { amountCents, creditMicros: _, ...transaction } = json;
30
+ return { ...transaction, amount: amountCents / 100 };
31
+ },
32
+ };
33
+ export async function get(token) {
34
+ const value = await request(token, '/billing');
35
+ const { balanceMicros, autoReload, recentTransactions, ...summary } = value;
36
+ return {
37
+ ...summary,
38
+ balance: balanceMicros / 1000000,
39
+ autoReload: AutoReload.fromJSON(autoReload),
40
+ subscription: summary.subscription ?? { plan: 'free', status: 'none', cancelAtPeriodEnd: false, addons: [] },
41
+ recentTransactions: recentTransactions.map((row) => Transaction.fromJSON(row)),
42
+ };
43
+ }
44
+ /** Start a Stripe Checkout top-up for the caller's personal wallet. Stripe
45
+ * returns to `returnUrl` when it is a trusted app origin, else the Fauna app. */
46
+ export function checkout(token, amount, options = {}) {
47
+ const amountCents = Math.round(amount * 100);
48
+ if (!Number.isSafeInteger(amountCents))
49
+ throw new Error('Invalid top-up amount.');
50
+ return request(token, '/billing/checkout', {
51
+ method: 'POST',
52
+ headers: { 'Content-Type': 'application/json' },
53
+ body: JSON.stringify({ amountCents, returnUrl: options.returnUrl }),
54
+ });
55
+ }
56
+ export function setupPaymentMethod(token, options = {}) {
57
+ return request(token, '/billing/payment-method/setup', {
58
+ method: 'POST',
59
+ headers: { 'Content-Type': 'application/json' },
60
+ body: JSON.stringify({ returnUrl: options.returnUrl }),
61
+ });
62
+ }
63
+ export async function updateAutoReload(token, update) {
64
+ const value = await request(token, '/billing/auto-reload', {
65
+ method: 'PATCH',
66
+ headers: { 'Content-Type': 'application/json' },
67
+ body: JSON.stringify({
68
+ enabled: update.enabled,
69
+ thresholdCents: Math.round(update.threshold * 100),
70
+ targetCents: Math.round(update.target * 100),
71
+ monthlyCapCents: Math.round(update.monthlyCap * 100),
72
+ }),
73
+ });
74
+ return AutoReload.fromJSON(value);
75
+ }
76
+ export function openPortal(token, options = {}) {
77
+ return request(token, '/billing/payment-method/portal', {
78
+ method: 'POST',
79
+ headers: { 'Content-Type': 'application/json' },
80
+ body: JSON.stringify({ returnUrl: options.returnUrl }),
81
+ });
82
+ }
83
+ /**
84
+ * Subscribe to Premium, or with `addon`, buy an add-on. With a subscription
85
+ * already, the add-on joins it at once (the URL then returns to `returnUrl`, or
86
+ * opens the invoice when the card needs confirming); otherwise Checkout sells
87
+ * Premium and the add-on together. Open the returned URL.
88
+ */
89
+ export function subscribe(token, options = {}) {
90
+ return request(token, '/billing/subscription/checkout', {
91
+ method: 'POST',
92
+ headers: { 'Content-Type': 'application/json' },
93
+ body: JSON.stringify({ returnUrl: options.returnUrl, addon: options.addon }),
94
+ });
95
+ }
96
+ /** End Premium at the period's end (add-ons end with it), or with `addon`, stop that add-on now with a prorated credit. */
97
+ export function cancelSubscription(token, options = {}) {
98
+ return request(token, '/billing/subscription/cancel', {
99
+ method: 'POST',
100
+ headers: { 'Content-Type': 'application/json' },
101
+ body: JSON.stringify({ addon: options.addon }),
102
+ });
103
+ }
104
+ /** The add-ons for sale. */
105
+ export function addons(token) {
106
+ return request(token, '/billing/addons');
107
+ }
108
+ export async function transactions(token, query) {
109
+ const params = new URLSearchParams({ cursor: String(query.cursor), max: String(query.max) });
110
+ const rows = await request(token, `/billing/transactions?${params}`);
111
+ return rows.map((row) => Transaction.fromJSON(row));
112
+ }
113
+ /** Daily base/spread usage for the caller's wallet, or a group's (member-only).
114
+ * `days` bounds the window server-side (default 30, max 180). */
115
+ export async function usage(token, groupId, days = 30) {
116
+ const query = new URLSearchParams({ days: String(days) });
117
+ if (groupId)
118
+ query.set('groupId', groupId);
119
+ const value = await request(token, `/billing/usage?${query.toString()}`);
120
+ return {
121
+ principalKind: value.principalKind,
122
+ since: value.since,
123
+ usage: value.usage.map((u) => ({ day: u.day, service: u.service, base: u.baseMicros / 1000000, spread: u.spreadMicros / 1000000 })),
124
+ };
125
+ }
126
+ export function format(amount) {
127
+ return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(amount);
128
+ }
@@ -0,0 +1,83 @@
1
+ import { ApiKey, Credentials } from "./authentification.js";
2
+ import { Group } from "./groups.js";
3
+ /** An invitee's answer. Everyone starts at `invited`. */
4
+ export type Answer = 'invited' | 'yes' | 'no' | 'maybe';
5
+ export type Attendee = {
6
+ email: string;
7
+ response: Answer;
8
+ };
9
+ /** Whose calendar an event is on. */
10
+ export type CalendarKind = 'person' | 'group';
11
+ export type Calendar = {
12
+ kind: CalendarKind;
13
+ id: string;
14
+ name: string;
15
+ };
16
+ /** A linked calendar an event syncs from. */
17
+ export type Source = 'google' | 'microsoft';
18
+ export type Event = {
19
+ id: string;
20
+ /** The calendar it's on: yours, a group's, or whoever invited you. */
21
+ calendar: Calendar;
22
+ title: string;
23
+ notes: string;
24
+ place: string;
25
+ /** For a recurring event in a list, this occurrence's. */
26
+ start: Date;
27
+ end: Date;
28
+ allDay: boolean;
29
+ /** An IANA time zone, like America/New_York. A recurring event repeats on its clock. */
30
+ timeZone: string;
31
+ /** An RRULE without its prefix, like FREQ=WEEKLY;BYDAY=MO. Empty for a one off. */
32
+ recurrence: string;
33
+ /** Minutes before each start to email a reminder; 0 for none. */
34
+ reminder: number;
35
+ attendees: Attendee[];
36
+ /** A linked Google or Outlook calendar it syncs from, or null for Fauna's own. */
37
+ source: Source | null;
38
+ /** You can change it: it's on your calendar or one of your groups'. */
39
+ editable: boolean;
40
+ /** Your answer, when you're invited. */
41
+ response?: Answer;
42
+ created: Date;
43
+ updated: Date;
44
+ };
45
+ /** A new event. Only `title`, `start` and `end` are required. */
46
+ export type Draft = {
47
+ title: string;
48
+ start: Date;
49
+ end: Date;
50
+ allDay?: boolean;
51
+ /** UTC by default. */
52
+ timeZone?: string;
53
+ place?: string;
54
+ notes?: string;
55
+ recurrence?: string;
56
+ /** 10 minutes by default; 0 for none. */
57
+ reminder?: number;
58
+ /** Emails to invite. Each gets an invitation. */
59
+ attendees?: string[];
60
+ };
61
+ /**
62
+ * Events between `from` and `to` (at most 400 days apart), one per occurrence,
63
+ * soonest first. With `group`, that group's calendar; otherwise yours, your
64
+ * groups' and the events you're invited to.
65
+ */
66
+ export declare function list(authorization: Credentials | ApiKey, from: Date, to: Date, group?: Group): Promise<Event[]>;
67
+ /** One event on your calendar, a group's, or one you're invited to. */
68
+ export declare function get(authorization: Credentials | ApiKey, id: string): Promise<Event>;
69
+ /** Put an event on your calendar, or `group`'s, and email everyone it invites. */
70
+ export declare function create(authorization: Credentials | ApiKey, draft: Draft, group?: Group): Promise<Event>;
71
+ /**
72
+ * Change an event on your calendar or a group's. A recurring event changes as a
73
+ * whole. New invitees get invited; the rest hear about a new time or place.
74
+ */
75
+ export declare function update(authorization: Credentials | ApiKey, id: string, update: Partial<Draft>): Promise<Event>;
76
+ /** Remove an event from your calendar or a group's, telling everyone it invited. */
77
+ export declare function remove(authorization: Credentials | ApiKey, id: string): Promise<void>;
78
+ /** Answer an event you're invited to: `yes`, `no` or `maybe`. */
79
+ export declare function respond(authorization: Credentials | ApiKey, id: string, answer: Answer): Promise<Event>;
80
+ /** The secret .ics link for your calendar, or `group`'s, for Google, Apple or Outlook to subscribe to. */
81
+ export declare function feed(authorization: Credentials | ApiKey, group?: Group): Promise<string>;
82
+ /** A new .ics link for your calendar, or `group`'s. The old one stops working. */
83
+ export declare function resetFeed(credentials: Credentials, group?: Group): Promise<string>;