@qmuse/appwrite-runtime-sdk 0.0.1-dev.1
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.
- package/LEGAL.md +7 -0
- package/README.md +81 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/runtime.d.ts +31 -0
- package/dist/runtime.js +939 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.js +1 -0
- package/package.json +95 -0
package/LEGAL.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Legal Disclaimer
|
|
2
|
+
|
|
3
|
+
Within this source code, the comments in Chinese shall be the original, governing version. Any comment in other languages are for reference only. In the event of any conflict between the Chinese language version comments and other language version comments, the Chinese language version shall prevail.
|
|
4
|
+
|
|
5
|
+
法律免责声明
|
|
6
|
+
|
|
7
|
+
关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# QMuse Appwrite Runtime SDK
|
|
2
|
+
|
|
3
|
+
浏览器端 QMuse Appwrite Runtime SDK。应用仍在 `src/services/appwrite.ts` 创建唯一的 Appwrite `Client`、`Account` 和 `TablesDB`,本包负责 Runtime 配置、Session 协调、单次 401 刷新和带审计字段保护的 TablesDB CRUD。
|
|
4
|
+
|
|
5
|
+
## 使用
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Account, Client, TablesDB } from 'appwrite';
|
|
9
|
+
import { createQmuseAppwriteRuntime } from '@alipay/appwrite-runtime-sdk';
|
|
10
|
+
|
|
11
|
+
export const client = new Client();
|
|
12
|
+
export const account = new Account(client);
|
|
13
|
+
export const tablesDB = new TablesDB(client);
|
|
14
|
+
|
|
15
|
+
const runtime = createQmuseAppwriteRuntime({
|
|
16
|
+
client,
|
|
17
|
+
account,
|
|
18
|
+
tablesDB,
|
|
19
|
+
getMuseRuntime: () => window.__MUSE__,
|
|
20
|
+
getQmuseLoginUserId: () => {
|
|
21
|
+
const userId = window.__TERN__?.user?.clientUser?.userId;
|
|
22
|
+
return typeof userId === 'string' && userId.trim() ? userId : null;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const initAppwrite = runtime.init;
|
|
27
|
+
export const getAppwriteClients = runtime.getClients;
|
|
28
|
+
export const createQmuseRow = runtime.createRow;
|
|
29
|
+
export const updateQmuseRow = runtime.updateRow;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
不要在页面、hook 或业务服务中直接创建 Appwrite SDK 实例,或绕过 `createRow` / `updateRow` 写入审计字段。
|
|
33
|
+
|
|
34
|
+
新增、更新和删除会由 SDK 内置地异步上报最小审计事件;上报不阻塞 CRUD,也不向业务代码暴露 hook、开关或 API Key。SDK 不上传业务 data、custom-token 或 Session。项目内置的 Appwrite 事件云函数仍负责成功 mutation 的可靠服务端审计与重试;浏览器侧上报只用于补充安全信号。
|
|
35
|
+
|
|
36
|
+
## Badges
|
|
37
|
+
|
|
38
|
+
[![TNPM version][tnpm-image]][tnpm-url]
|
|
39
|
+
[![TNPM downloads][tnpm-downloads-image]][tnpm-url]
|
|
40
|
+

|
|
41
|
+
|
|
42
|
+
[tnpm-image]: https://npm.alibaba-inc.com/badge/v/@alipay/appwrite-runtime-sdk.svg
|
|
43
|
+
[tnpm-url]: https://npm.alibaba-inc.com/package/@alipay/appwrite-runtime-sdk
|
|
44
|
+
[tnpm-downloads-image]: https://npm.alibaba-inc.com/badge/d/@alipay/appwrite-runtime-sdk.svg
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 准备工作
|
|
49
|
+
|
|
50
|
+
- 安装 [nodejs](https://nodejs.org/en/)
|
|
51
|
+
- 使用最新版本的 [tnpm](http://web.npm.alibaba-inc.com/)
|
|
52
|
+
|
|
53
|
+
## 开发
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# 安装依赖
|
|
57
|
+
$ tnpm install
|
|
58
|
+
|
|
59
|
+
# 构建与检查
|
|
60
|
+
$ npm run build
|
|
61
|
+
$ npm run lint
|
|
62
|
+
$ npm test
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 发布
|
|
66
|
+
|
|
67
|
+
该 SDK 不发布为海兔资产;构建、测试和公网 npm 发布均使用独立工具链。
|
|
68
|
+
|
|
69
|
+
### 公网 `@qmuse` 包
|
|
70
|
+
|
|
71
|
+
先在 `package.json` 设定未发布的正式语义化版本,再执行:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npm run publish:dev
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
脚本会验证 npmjs 登录态、检查版本是否已存在,并运行 CI 与构建。确认后,它只在发布期间把包名临时改为 `@qmuse/appwrite-runtime-sdk`,发布到 npmjs 的 `dev` tag,退出时恢复本地 `package.json`。验证通过后,可将该版本转为 `latest`:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm dist-tag add @qmuse/appwrite-runtime-sdk@<version> latest --registry https://registry.npmjs.org/
|
|
81
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { createQmuseAppwriteRuntime } from './runtime';
|
|
2
|
+
export type { AppwriteAccountLike, AppwriteClientLike, AppwriteError, AppwriteRuntimeConfig, AppwriteSession, AppwriteTablesDBLike, AppwriteUser, BusinessRowData, QmuseAppwriteClients, QmuseAppwriteRuntimeOptions, QmuseEnvironment, QmuseRuntimeContext, } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createQmuseAppwriteRuntime } from "./runtime";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AppwriteUser, BusinessRowData, QmuseAppwriteClients, QmuseAppwriteRuntimeOptions } from './types';
|
|
2
|
+
export declare function createQmuseAppwriteRuntime(options: QmuseAppwriteRuntimeOptions): {
|
|
3
|
+
init: () => Promise<void>;
|
|
4
|
+
getClients: () => Promise<QmuseAppwriteClients>;
|
|
5
|
+
getCurrentUser: () => AppwriteUser;
|
|
6
|
+
refreshSession: () => Promise<void>;
|
|
7
|
+
listRows: ({ tableId, queries, limit, cursor, }: {
|
|
8
|
+
tableId: string;
|
|
9
|
+
queries?: string[] | undefined;
|
|
10
|
+
limit?: number | undefined;
|
|
11
|
+
cursor?: string | undefined;
|
|
12
|
+
}) => Promise<unknown>;
|
|
13
|
+
getRow: ({ tableId, rowId, }: {
|
|
14
|
+
tableId: string;
|
|
15
|
+
rowId: string;
|
|
16
|
+
}) => Promise<unknown>;
|
|
17
|
+
createRow: ({ tableId, data, rowId, }: {
|
|
18
|
+
tableId: string;
|
|
19
|
+
data: BusinessRowData;
|
|
20
|
+
rowId?: string | undefined;
|
|
21
|
+
}) => Promise<unknown>;
|
|
22
|
+
updateRow: ({ tableId, rowId, data, }: {
|
|
23
|
+
tableId: string;
|
|
24
|
+
rowId: string;
|
|
25
|
+
data: BusinessRowData;
|
|
26
|
+
}) => Promise<unknown>;
|
|
27
|
+
deleteRow: ({ tableId, rowId, }: {
|
|
28
|
+
tableId: string;
|
|
29
|
+
rowId: string;
|
|
30
|
+
}) => Promise<unknown>;
|
|
31
|
+
};
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
3
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
4
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
5
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
6
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
7
|
+
import { ID, Permission, Query, Role } from 'appwrite';
|
|
8
|
+
var BLOCKED_MUTATION_FIELDS = new Set(['code', 'creatorId', 'modifierId', 'gmtCreate', 'gmtModified']);
|
|
9
|
+
function isRecord(value) {
|
|
10
|
+
return Boolean(value) && _typeof(value) === 'object' && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
function getErrorCode(error) {
|
|
13
|
+
if (!isRecord(error)) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
var code = Number(error.code);
|
|
17
|
+
return Number.isFinite(code) ? code : null;
|
|
18
|
+
}
|
|
19
|
+
function createResponseError(_ref) {
|
|
20
|
+
var _payload$code, _payload$type;
|
|
21
|
+
var payload = _ref.payload,
|
|
22
|
+
status = _ref.status,
|
|
23
|
+
fallbackMessage = _ref.fallbackMessage,
|
|
24
|
+
fallbackType = _ref.fallbackType;
|
|
25
|
+
var error = new Error(typeof (payload === null || payload === void 0 ? void 0 : payload.message) === 'string' && payload.message ? payload.message : fallbackMessage);
|
|
26
|
+
Object.assign(error, {
|
|
27
|
+
code: (_payload$code = payload === null || payload === void 0 ? void 0 : payload.code) !== null && _payload$code !== void 0 ? _payload$code : status,
|
|
28
|
+
type: (_payload$type = payload === null || payload === void 0 ? void 0 : payload.type) !== null && _payload$type !== void 0 ? _payload$type : fallbackType
|
|
29
|
+
});
|
|
30
|
+
return error;
|
|
31
|
+
}
|
|
32
|
+
function readResponsePayload(_x) {
|
|
33
|
+
return _readResponsePayload.apply(this, arguments);
|
|
34
|
+
}
|
|
35
|
+
function _readResponsePayload() {
|
|
36
|
+
_readResponsePayload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21(response) {
|
|
37
|
+
var payload;
|
|
38
|
+
return _regeneratorRuntime().wrap(function _callee21$(_context21) {
|
|
39
|
+
while (1) switch (_context21.prev = _context21.next) {
|
|
40
|
+
case 0:
|
|
41
|
+
_context21.next = 2;
|
|
42
|
+
return response.json().catch(function () {
|
|
43
|
+
return null;
|
|
44
|
+
});
|
|
45
|
+
case 2:
|
|
46
|
+
payload = _context21.sent;
|
|
47
|
+
return _context21.abrupt("return", isRecord(payload) ? payload : null);
|
|
48
|
+
case 4:
|
|
49
|
+
case "end":
|
|
50
|
+
return _context21.stop();
|
|
51
|
+
}
|
|
52
|
+
}, _callee21);
|
|
53
|
+
}));
|
|
54
|
+
return _readResponsePayload.apply(this, arguments);
|
|
55
|
+
}
|
|
56
|
+
function sanitizeBusinessRowData(data) {
|
|
57
|
+
return Object.fromEntries(Object.entries(data).filter(function (_ref2) {
|
|
58
|
+
var _ref3 = _slicedToArray(_ref2, 1),
|
|
59
|
+
key = _ref3[0];
|
|
60
|
+
return !key.startsWith('$') && !BLOCKED_MUTATION_FIELDS.has(key);
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
export function createQmuseAppwriteRuntime(options) {
|
|
64
|
+
var _options$fetch;
|
|
65
|
+
var client = options.client,
|
|
66
|
+
account = options.account,
|
|
67
|
+
tablesDB = options.tablesDB;
|
|
68
|
+
var request = (_options$fetch = options.fetch) !== null && _options$fetch !== void 0 ? _options$fetch : globalThis.fetch;
|
|
69
|
+
if (!request) {
|
|
70
|
+
throw new Error('A fetch implementation is required');
|
|
71
|
+
}
|
|
72
|
+
var state = {
|
|
73
|
+
runtimeConfig: null,
|
|
74
|
+
currentUser: null,
|
|
75
|
+
currentSessionProvider: null,
|
|
76
|
+
confirmedQmuseUserId: null
|
|
77
|
+
};
|
|
78
|
+
var initPromise = null;
|
|
79
|
+
var sessionSyncPromise = null;
|
|
80
|
+
var refreshPromise = null;
|
|
81
|
+
function getMuseRuntime() {
|
|
82
|
+
var runtime = options.getMuseRuntime();
|
|
83
|
+
if (!runtime || runtime.env !== 'dev' && runtime.env !== 'prod') {
|
|
84
|
+
throw new Error('QMuse runtime env must be dev or prod');
|
|
85
|
+
}
|
|
86
|
+
return runtime;
|
|
87
|
+
}
|
|
88
|
+
function getRuntimeConfig() {
|
|
89
|
+
if (!state.runtimeConfig) {
|
|
90
|
+
throw new Error('Appwrite is not initialized');
|
|
91
|
+
}
|
|
92
|
+
return state.runtimeConfig;
|
|
93
|
+
}
|
|
94
|
+
function clearCurrentSession() {
|
|
95
|
+
state.currentUser = null;
|
|
96
|
+
state.currentSessionProvider = null;
|
|
97
|
+
state.confirmedQmuseUserId = null;
|
|
98
|
+
}
|
|
99
|
+
function loadAppwriteRuntimeConfig() {
|
|
100
|
+
return _loadAppwriteRuntimeConfig.apply(this, arguments);
|
|
101
|
+
}
|
|
102
|
+
function _loadAppwriteRuntimeConfig() {
|
|
103
|
+
_loadAppwriteRuntimeConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
104
|
+
var runtime, response, payload, data, config;
|
|
105
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
106
|
+
while (1) switch (_context.prev = _context.next) {
|
|
107
|
+
case 0:
|
|
108
|
+
runtime = getMuseRuntime();
|
|
109
|
+
_context.next = 3;
|
|
110
|
+
return request(new URL('/runtime/appwrite/config', runtime.domain).toString(), {
|
|
111
|
+
method: 'POST',
|
|
112
|
+
credentials: 'include',
|
|
113
|
+
headers: {
|
|
114
|
+
'Content-Type': 'application/json'
|
|
115
|
+
},
|
|
116
|
+
body: JSON.stringify({
|
|
117
|
+
appId: runtime.appId,
|
|
118
|
+
env: runtime.env
|
|
119
|
+
})
|
|
120
|
+
});
|
|
121
|
+
case 3:
|
|
122
|
+
response = _context.sent;
|
|
123
|
+
_context.next = 6;
|
|
124
|
+
return readResponsePayload(response);
|
|
125
|
+
case 6:
|
|
126
|
+
payload = _context.sent;
|
|
127
|
+
data = isRecord(payload === null || payload === void 0 ? void 0 : payload.data) ? payload.data : null;
|
|
128
|
+
if (!(!response.ok || (payload === null || payload === void 0 ? void 0 : payload.success) !== true || !data)) {
|
|
129
|
+
_context.next = 10;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
throw createResponseError({
|
|
133
|
+
payload: payload,
|
|
134
|
+
status: response.status,
|
|
135
|
+
fallbackMessage: "Appwrite config request failed: ".concat(response.status),
|
|
136
|
+
fallbackType: 'appwrite_config_error'
|
|
137
|
+
});
|
|
138
|
+
case 10:
|
|
139
|
+
config = {
|
|
140
|
+
endpoint: data.endpoint,
|
|
141
|
+
projectId: data.projectId,
|
|
142
|
+
databaseId: data.databaseId,
|
|
143
|
+
authTokenEndpoint: data.authTokenEndpoint
|
|
144
|
+
};
|
|
145
|
+
if (!(typeof config.endpoint !== 'string' || !config.endpoint || typeof config.projectId !== 'string' || !config.projectId || typeof config.databaseId !== 'string' || !config.databaseId || typeof config.authTokenEndpoint !== 'string' || !config.authTokenEndpoint)) {
|
|
146
|
+
_context.next = 13;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
throw new Error('Appwrite runtime config is incomplete');
|
|
150
|
+
case 13:
|
|
151
|
+
return _context.abrupt("return", config);
|
|
152
|
+
case 14:
|
|
153
|
+
case "end":
|
|
154
|
+
return _context.stop();
|
|
155
|
+
}
|
|
156
|
+
}, _callee);
|
|
157
|
+
}));
|
|
158
|
+
return _loadAppwriteRuntimeConfig.apply(this, arguments);
|
|
159
|
+
}
|
|
160
|
+
function cacheCurrentSession() {
|
|
161
|
+
return _cacheCurrentSession.apply(this, arguments);
|
|
162
|
+
}
|
|
163
|
+
function _cacheCurrentSession() {
|
|
164
|
+
_cacheCurrentSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
165
|
+
var _yield$Promise$all, _yield$Promise$all2, user, session;
|
|
166
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
167
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
168
|
+
case 0:
|
|
169
|
+
_context2.next = 2;
|
|
170
|
+
return Promise.all([account.get(), account.getSession({
|
|
171
|
+
sessionId: 'current'
|
|
172
|
+
})]);
|
|
173
|
+
case 2:
|
|
174
|
+
_yield$Promise$all = _context2.sent;
|
|
175
|
+
_yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
|
|
176
|
+
user = _yield$Promise$all2[0];
|
|
177
|
+
session = _yield$Promise$all2[1];
|
|
178
|
+
state.currentUser = user;
|
|
179
|
+
state.currentSessionProvider = session.provider;
|
|
180
|
+
case 8:
|
|
181
|
+
case "end":
|
|
182
|
+
return _context2.stop();
|
|
183
|
+
}
|
|
184
|
+
}, _callee2);
|
|
185
|
+
}));
|
|
186
|
+
return _cacheCurrentSession.apply(this, arguments);
|
|
187
|
+
}
|
|
188
|
+
function tryCacheCurrentSession() {
|
|
189
|
+
return _tryCacheCurrentSession.apply(this, arguments);
|
|
190
|
+
}
|
|
191
|
+
function _tryCacheCurrentSession() {
|
|
192
|
+
_tryCacheCurrentSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
193
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
194
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
195
|
+
case 0:
|
|
196
|
+
_context3.prev = 0;
|
|
197
|
+
_context3.next = 3;
|
|
198
|
+
return cacheCurrentSession();
|
|
199
|
+
case 3:
|
|
200
|
+
return _context3.abrupt("return", true);
|
|
201
|
+
case 6:
|
|
202
|
+
_context3.prev = 6;
|
|
203
|
+
_context3.t0 = _context3["catch"](0);
|
|
204
|
+
if (!(getErrorCode(_context3.t0) !== 401)) {
|
|
205
|
+
_context3.next = 10;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
throw _context3.t0;
|
|
209
|
+
case 10:
|
|
210
|
+
clearCurrentSession();
|
|
211
|
+
return _context3.abrupt("return", false);
|
|
212
|
+
case 12:
|
|
213
|
+
case "end":
|
|
214
|
+
return _context3.stop();
|
|
215
|
+
}
|
|
216
|
+
}, _callee3, null, [[0, 6]]);
|
|
217
|
+
}));
|
|
218
|
+
return _tryCacheCurrentSession.apply(this, arguments);
|
|
219
|
+
}
|
|
220
|
+
function deleteCurrentSession() {
|
|
221
|
+
return _deleteCurrentSession.apply(this, arguments);
|
|
222
|
+
}
|
|
223
|
+
function _deleteCurrentSession() {
|
|
224
|
+
_deleteCurrentSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
225
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
226
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
227
|
+
case 0:
|
|
228
|
+
if (state.currentUser) {
|
|
229
|
+
_context4.next = 2;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
return _context4.abrupt("return");
|
|
233
|
+
case 2:
|
|
234
|
+
_context4.prev = 2;
|
|
235
|
+
_context4.next = 5;
|
|
236
|
+
return account.deleteSession({
|
|
237
|
+
sessionId: 'current'
|
|
238
|
+
});
|
|
239
|
+
case 5:
|
|
240
|
+
_context4.next = 11;
|
|
241
|
+
break;
|
|
242
|
+
case 7:
|
|
243
|
+
_context4.prev = 7;
|
|
244
|
+
_context4.t0 = _context4["catch"](2);
|
|
245
|
+
if (!(getErrorCode(_context4.t0) !== 401)) {
|
|
246
|
+
_context4.next = 11;
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
throw _context4.t0;
|
|
250
|
+
case 11:
|
|
251
|
+
_context4.prev = 11;
|
|
252
|
+
clearCurrentSession();
|
|
253
|
+
return _context4.finish(11);
|
|
254
|
+
case 14:
|
|
255
|
+
case "end":
|
|
256
|
+
return _context4.stop();
|
|
257
|
+
}
|
|
258
|
+
}, _callee4, null, [[2, 7, 11, 14]]);
|
|
259
|
+
}));
|
|
260
|
+
return _deleteCurrentSession.apply(this, arguments);
|
|
261
|
+
}
|
|
262
|
+
function ensureAnonymousSession() {
|
|
263
|
+
return _ensureAnonymousSession.apply(this, arguments);
|
|
264
|
+
}
|
|
265
|
+
function _ensureAnonymousSession() {
|
|
266
|
+
_ensureAnonymousSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
267
|
+
var forceReplace,
|
|
268
|
+
_args5 = arguments;
|
|
269
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
270
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
271
|
+
case 0:
|
|
272
|
+
forceReplace = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : false;
|
|
273
|
+
if (state.currentUser) {
|
|
274
|
+
_context5.next = 4;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
_context5.next = 4;
|
|
278
|
+
return tryCacheCurrentSession();
|
|
279
|
+
case 4:
|
|
280
|
+
if (!(!forceReplace && state.currentUser && state.currentSessionProvider === 'anonymous')) {
|
|
281
|
+
_context5.next = 7;
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
state.confirmedQmuseUserId = null;
|
|
285
|
+
return _context5.abrupt("return");
|
|
286
|
+
case 7:
|
|
287
|
+
_context5.next = 9;
|
|
288
|
+
return deleteCurrentSession();
|
|
289
|
+
case 9:
|
|
290
|
+
_context5.next = 11;
|
|
291
|
+
return account.createAnonymousSession();
|
|
292
|
+
case 11:
|
|
293
|
+
_context5.next = 13;
|
|
294
|
+
return cacheCurrentSession();
|
|
295
|
+
case 13:
|
|
296
|
+
state.confirmedQmuseUserId = null;
|
|
297
|
+
case 14:
|
|
298
|
+
case "end":
|
|
299
|
+
return _context5.stop();
|
|
300
|
+
}
|
|
301
|
+
}, _callee5);
|
|
302
|
+
}));
|
|
303
|
+
return _ensureAnonymousSession.apply(this, arguments);
|
|
304
|
+
}
|
|
305
|
+
function requestCustomToken() {
|
|
306
|
+
return _requestCustomToken.apply(this, arguments);
|
|
307
|
+
}
|
|
308
|
+
function _requestCustomToken() {
|
|
309
|
+
_requestCustomToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
310
|
+
var runtime, config, response, payload, data;
|
|
311
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
312
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
313
|
+
case 0:
|
|
314
|
+
runtime = getMuseRuntime();
|
|
315
|
+
config = getRuntimeConfig();
|
|
316
|
+
_context6.next = 4;
|
|
317
|
+
return request(new URL(config.authTokenEndpoint, runtime.domain).toString(), {
|
|
318
|
+
method: 'POST',
|
|
319
|
+
credentials: 'include',
|
|
320
|
+
headers: {
|
|
321
|
+
'Content-Type': 'application/json'
|
|
322
|
+
},
|
|
323
|
+
body: JSON.stringify({
|
|
324
|
+
appId: runtime.appId,
|
|
325
|
+
appVersionId: runtime.appVersionId,
|
|
326
|
+
env: runtime.env,
|
|
327
|
+
projectId: config.projectId
|
|
328
|
+
})
|
|
329
|
+
});
|
|
330
|
+
case 4:
|
|
331
|
+
response = _context6.sent;
|
|
332
|
+
if (!(response.status === 401)) {
|
|
333
|
+
_context6.next = 7;
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
return _context6.abrupt("return", null);
|
|
337
|
+
case 7:
|
|
338
|
+
_context6.next = 9;
|
|
339
|
+
return readResponsePayload(response);
|
|
340
|
+
case 9:
|
|
341
|
+
payload = _context6.sent;
|
|
342
|
+
data = isRecord(payload === null || payload === void 0 ? void 0 : payload.data) ? payload.data : null;
|
|
343
|
+
if (!(!response.ok || (payload === null || payload === void 0 ? void 0 : payload.success) !== true || !data)) {
|
|
344
|
+
_context6.next = 13;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
throw createResponseError({
|
|
348
|
+
payload: payload,
|
|
349
|
+
status: response.status,
|
|
350
|
+
fallbackMessage: "Custom token request failed: ".concat(response.status),
|
|
351
|
+
fallbackType: 'custom_token_error'
|
|
352
|
+
});
|
|
353
|
+
case 13:
|
|
354
|
+
if (!(typeof data.userId !== 'string' || !data.userId || typeof data.secret !== 'string' || !data.secret)) {
|
|
355
|
+
_context6.next = 15;
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
throw new Error('Custom token response is incomplete');
|
|
359
|
+
case 15:
|
|
360
|
+
return _context6.abrupt("return", {
|
|
361
|
+
userId: data.userId,
|
|
362
|
+
secret: data.secret
|
|
363
|
+
});
|
|
364
|
+
case 16:
|
|
365
|
+
case "end":
|
|
366
|
+
return _context6.stop();
|
|
367
|
+
}
|
|
368
|
+
}, _callee6);
|
|
369
|
+
}));
|
|
370
|
+
return _requestCustomToken.apply(this, arguments);
|
|
371
|
+
}
|
|
372
|
+
function ensureAuthenticatedSession(_x2) {
|
|
373
|
+
return _ensureAuthenticatedSession.apply(this, arguments);
|
|
374
|
+
}
|
|
375
|
+
function _ensureAuthenticatedSession() {
|
|
376
|
+
_ensureAuthenticatedSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref4) {
|
|
377
|
+
var qmuseUserId, userId, secret, _ref4$forceReplace, forceReplace;
|
|
378
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
379
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
380
|
+
case 0:
|
|
381
|
+
qmuseUserId = _ref4.qmuseUserId, userId = _ref4.userId, secret = _ref4.secret, _ref4$forceReplace = _ref4.forceReplace, forceReplace = _ref4$forceReplace === void 0 ? false : _ref4$forceReplace;
|
|
382
|
+
if (state.currentUser) {
|
|
383
|
+
_context7.next = 4;
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
_context7.next = 4;
|
|
387
|
+
return tryCacheCurrentSession();
|
|
388
|
+
case 4:
|
|
389
|
+
if (!(!forceReplace && state.currentUser && state.currentSessionProvider !== 'anonymous' && state.currentUser.$id === userId)) {
|
|
390
|
+
_context7.next = 7;
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
state.confirmedQmuseUserId = qmuseUserId;
|
|
394
|
+
return _context7.abrupt("return");
|
|
395
|
+
case 7:
|
|
396
|
+
_context7.next = 9;
|
|
397
|
+
return deleteCurrentSession();
|
|
398
|
+
case 9:
|
|
399
|
+
_context7.next = 11;
|
|
400
|
+
return account.createSession({
|
|
401
|
+
userId: userId,
|
|
402
|
+
secret: secret
|
|
403
|
+
});
|
|
404
|
+
case 11:
|
|
405
|
+
_context7.next = 13;
|
|
406
|
+
return cacheCurrentSession();
|
|
407
|
+
case 13:
|
|
408
|
+
state.confirmedQmuseUserId = qmuseUserId;
|
|
409
|
+
case 14:
|
|
410
|
+
case "end":
|
|
411
|
+
return _context7.stop();
|
|
412
|
+
}
|
|
413
|
+
}, _callee7);
|
|
414
|
+
}));
|
|
415
|
+
return _ensureAuthenticatedSession.apply(this, arguments);
|
|
416
|
+
}
|
|
417
|
+
function coordinateAppwriteSession() {
|
|
418
|
+
return _coordinateAppwriteSession.apply(this, arguments);
|
|
419
|
+
}
|
|
420
|
+
function _coordinateAppwriteSession() {
|
|
421
|
+
_coordinateAppwriteSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
|
|
422
|
+
var force,
|
|
423
|
+
qmuseUserId,
|
|
424
|
+
sessionMatchesRuntime,
|
|
425
|
+
token,
|
|
426
|
+
_args8 = arguments;
|
|
427
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
428
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
429
|
+
case 0:
|
|
430
|
+
force = _args8.length > 0 && _args8[0] !== undefined ? _args8[0] : false;
|
|
431
|
+
qmuseUserId = options.getQmuseLoginUserId();
|
|
432
|
+
sessionMatchesRuntime = state.currentUser !== null && (qmuseUserId ? state.currentSessionProvider !== 'anonymous' && state.confirmedQmuseUserId === qmuseUserId : state.currentSessionProvider === 'anonymous' && state.confirmedQmuseUserId === null);
|
|
433
|
+
if (!(!force && sessionMatchesRuntime)) {
|
|
434
|
+
_context8.next = 5;
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
return _context8.abrupt("return");
|
|
438
|
+
case 5:
|
|
439
|
+
if (state.currentUser) {
|
|
440
|
+
_context8.next = 8;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
_context8.next = 8;
|
|
444
|
+
return tryCacheCurrentSession();
|
|
445
|
+
case 8:
|
|
446
|
+
if (qmuseUserId) {
|
|
447
|
+
_context8.next = 12;
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
_context8.next = 11;
|
|
451
|
+
return ensureAnonymousSession(force);
|
|
452
|
+
case 11:
|
|
453
|
+
return _context8.abrupt("return");
|
|
454
|
+
case 12:
|
|
455
|
+
if (!(!force && state.currentUser && state.currentSessionProvider !== 'anonymous' && state.confirmedQmuseUserId === qmuseUserId)) {
|
|
456
|
+
_context8.next = 14;
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
return _context8.abrupt("return");
|
|
460
|
+
case 14:
|
|
461
|
+
_context8.next = 16;
|
|
462
|
+
return requestCustomToken();
|
|
463
|
+
case 16:
|
|
464
|
+
token = _context8.sent;
|
|
465
|
+
if (token) {
|
|
466
|
+
_context8.next = 21;
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
_context8.next = 20;
|
|
470
|
+
return ensureAnonymousSession(force);
|
|
471
|
+
case 20:
|
|
472
|
+
return _context8.abrupt("return");
|
|
473
|
+
case 21:
|
|
474
|
+
_context8.next = 23;
|
|
475
|
+
return ensureAuthenticatedSession(_objectSpread(_objectSpread({
|
|
476
|
+
qmuseUserId: qmuseUserId
|
|
477
|
+
}, token), {}, {
|
|
478
|
+
forceReplace: force
|
|
479
|
+
}));
|
|
480
|
+
case 23:
|
|
481
|
+
case "end":
|
|
482
|
+
return _context8.stop();
|
|
483
|
+
}
|
|
484
|
+
}, _callee8);
|
|
485
|
+
}));
|
|
486
|
+
return _coordinateAppwriteSession.apply(this, arguments);
|
|
487
|
+
}
|
|
488
|
+
function synchronizeAppwriteSession() {
|
|
489
|
+
return _synchronizeAppwriteSession.apply(this, arguments);
|
|
490
|
+
}
|
|
491
|
+
function _synchronizeAppwriteSession() {
|
|
492
|
+
_synchronizeAppwriteSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
|
|
493
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
494
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
495
|
+
case 0:
|
|
496
|
+
if (!sessionSyncPromise) {
|
|
497
|
+
sessionSyncPromise = coordinateAppwriteSession().finally(function () {
|
|
498
|
+
sessionSyncPromise = null;
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
return _context9.abrupt("return", sessionSyncPromise);
|
|
502
|
+
case 2:
|
|
503
|
+
case "end":
|
|
504
|
+
return _context9.stop();
|
|
505
|
+
}
|
|
506
|
+
}, _callee9);
|
|
507
|
+
}));
|
|
508
|
+
return _synchronizeAppwriteSession.apply(this, arguments);
|
|
509
|
+
}
|
|
510
|
+
function initializeAppwrite() {
|
|
511
|
+
return _initializeAppwrite.apply(this, arguments);
|
|
512
|
+
}
|
|
513
|
+
function _initializeAppwrite() {
|
|
514
|
+
_initializeAppwrite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
|
|
515
|
+
var config;
|
|
516
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
517
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
518
|
+
case 0:
|
|
519
|
+
_context10.next = 2;
|
|
520
|
+
return loadAppwriteRuntimeConfig();
|
|
521
|
+
case 2:
|
|
522
|
+
config = _context10.sent;
|
|
523
|
+
client.setEndpoint(config.endpoint).setProject(config.projectId);
|
|
524
|
+
state.runtimeConfig = config;
|
|
525
|
+
_context10.next = 7;
|
|
526
|
+
return synchronizeAppwriteSession();
|
|
527
|
+
case 7:
|
|
528
|
+
case "end":
|
|
529
|
+
return _context10.stop();
|
|
530
|
+
}
|
|
531
|
+
}, _callee10);
|
|
532
|
+
}));
|
|
533
|
+
return _initializeAppwrite.apply(this, arguments);
|
|
534
|
+
}
|
|
535
|
+
function init() {
|
|
536
|
+
if (!initPromise) {
|
|
537
|
+
initPromise = initializeAppwrite().catch(function (error) {
|
|
538
|
+
initPromise = null;
|
|
539
|
+
state.runtimeConfig = null;
|
|
540
|
+
clearCurrentSession();
|
|
541
|
+
throw error;
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
return initPromise;
|
|
545
|
+
}
|
|
546
|
+
function getClients() {
|
|
547
|
+
return _getClients.apply(this, arguments);
|
|
548
|
+
}
|
|
549
|
+
function _getClients() {
|
|
550
|
+
_getClients = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
|
|
551
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
552
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
553
|
+
case 0:
|
|
554
|
+
_context11.next = 2;
|
|
555
|
+
return init();
|
|
556
|
+
case 2:
|
|
557
|
+
return _context11.abrupt("return", {
|
|
558
|
+
client: client,
|
|
559
|
+
account: account,
|
|
560
|
+
tablesDB: tablesDB,
|
|
561
|
+
databaseId: getRuntimeConfig().databaseId
|
|
562
|
+
});
|
|
563
|
+
case 3:
|
|
564
|
+
case "end":
|
|
565
|
+
return _context11.stop();
|
|
566
|
+
}
|
|
567
|
+
}, _callee11);
|
|
568
|
+
}));
|
|
569
|
+
return _getClients.apply(this, arguments);
|
|
570
|
+
}
|
|
571
|
+
function getCurrentUser() {
|
|
572
|
+
if (!state.currentUser) {
|
|
573
|
+
throw new Error('Appwrite user is not authenticated');
|
|
574
|
+
}
|
|
575
|
+
return state.currentUser;
|
|
576
|
+
}
|
|
577
|
+
function refreshSession() {
|
|
578
|
+
return _refreshSession.apply(this, arguments);
|
|
579
|
+
}
|
|
580
|
+
function _refreshSession() {
|
|
581
|
+
_refreshSession = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
|
|
582
|
+
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
|
|
583
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
584
|
+
case 0:
|
|
585
|
+
_context13.next = 2;
|
|
586
|
+
return init();
|
|
587
|
+
case 2:
|
|
588
|
+
if (!refreshPromise) {
|
|
589
|
+
refreshPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
590
|
+
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
591
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
592
|
+
case 0:
|
|
593
|
+
if (!sessionSyncPromise) {
|
|
594
|
+
_context12.next = 3;
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
_context12.next = 3;
|
|
598
|
+
return sessionSyncPromise;
|
|
599
|
+
case 3:
|
|
600
|
+
_context12.next = 5;
|
|
601
|
+
return coordinateAppwriteSession(true);
|
|
602
|
+
case 5:
|
|
603
|
+
case "end":
|
|
604
|
+
return _context12.stop();
|
|
605
|
+
}
|
|
606
|
+
}, _callee12);
|
|
607
|
+
}))().finally(function () {
|
|
608
|
+
refreshPromise = null;
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
return _context13.abrupt("return", refreshPromise);
|
|
612
|
+
case 4:
|
|
613
|
+
case "end":
|
|
614
|
+
return _context13.stop();
|
|
615
|
+
}
|
|
616
|
+
}, _callee13);
|
|
617
|
+
}));
|
|
618
|
+
return _refreshSession.apply(this, arguments);
|
|
619
|
+
}
|
|
620
|
+
function withSessionRetry(_x3) {
|
|
621
|
+
return _withSessionRetry.apply(this, arguments);
|
|
622
|
+
}
|
|
623
|
+
function _withSessionRetry() {
|
|
624
|
+
_withSessionRetry = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(operation) {
|
|
625
|
+
return _regeneratorRuntime().wrap(function _callee14$(_context14) {
|
|
626
|
+
while (1) switch (_context14.prev = _context14.next) {
|
|
627
|
+
case 0:
|
|
628
|
+
_context14.next = 2;
|
|
629
|
+
return init();
|
|
630
|
+
case 2:
|
|
631
|
+
_context14.next = 4;
|
|
632
|
+
return synchronizeAppwriteSession();
|
|
633
|
+
case 4:
|
|
634
|
+
_context14.prev = 4;
|
|
635
|
+
_context14.next = 7;
|
|
636
|
+
return operation();
|
|
637
|
+
case 7:
|
|
638
|
+
return _context14.abrupt("return", _context14.sent);
|
|
639
|
+
case 10:
|
|
640
|
+
_context14.prev = 10;
|
|
641
|
+
_context14.t0 = _context14["catch"](4);
|
|
642
|
+
if (!(getErrorCode(_context14.t0) !== 401)) {
|
|
643
|
+
_context14.next = 14;
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
throw _context14.t0;
|
|
647
|
+
case 14:
|
|
648
|
+
_context14.next = 16;
|
|
649
|
+
return refreshSession();
|
|
650
|
+
case 16:
|
|
651
|
+
return _context14.abrupt("return", operation());
|
|
652
|
+
case 17:
|
|
653
|
+
case "end":
|
|
654
|
+
return _context14.stop();
|
|
655
|
+
}
|
|
656
|
+
}, _callee14, null, [[4, 10]]);
|
|
657
|
+
}));
|
|
658
|
+
return _withSessionRetry.apply(this, arguments);
|
|
659
|
+
}
|
|
660
|
+
function scheduleMutationAudit(_ref5) {
|
|
661
|
+
var operation = _ref5.operation,
|
|
662
|
+
tableId = _ref5.tableId,
|
|
663
|
+
rowId = _ref5.rowId,
|
|
664
|
+
outcome = _ref5.outcome,
|
|
665
|
+
durationMs = _ref5.durationMs,
|
|
666
|
+
errorCode = _ref5.errorCode;
|
|
667
|
+
var runtime = getMuseRuntime();
|
|
668
|
+
globalThis.setTimeout(function () {
|
|
669
|
+
try {
|
|
670
|
+
void Promise.resolve(request(new URL('/runtime/appwrite/mutation-events', runtime.domain).toString(), {
|
|
671
|
+
method: 'POST',
|
|
672
|
+
credentials: 'include',
|
|
673
|
+
keepalive: true,
|
|
674
|
+
headers: {
|
|
675
|
+
'Content-Type': 'application/json'
|
|
676
|
+
},
|
|
677
|
+
body: JSON.stringify(_objectSpread({
|
|
678
|
+
schemaVersion: 1,
|
|
679
|
+
operation: operation,
|
|
680
|
+
outcome: outcome,
|
|
681
|
+
appId: runtime.appId,
|
|
682
|
+
env: runtime.env,
|
|
683
|
+
tableId: tableId,
|
|
684
|
+
rowId: rowId,
|
|
685
|
+
durationMs: durationMs
|
|
686
|
+
}, errorCode === undefined ? {} : {
|
|
687
|
+
errorCode: errorCode
|
|
688
|
+
}))
|
|
689
|
+
})).catch(function () {
|
|
690
|
+
return undefined;
|
|
691
|
+
});
|
|
692
|
+
} catch (_unused) {
|
|
693
|
+
// Security telemetry is best-effort and cannot alter mutation semantics.
|
|
694
|
+
}
|
|
695
|
+
}, 0);
|
|
696
|
+
}
|
|
697
|
+
function observeMutation(_x4) {
|
|
698
|
+
return _observeMutation.apply(this, arguments);
|
|
699
|
+
}
|
|
700
|
+
function _observeMutation() {
|
|
701
|
+
_observeMutation = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(_ref6) {
|
|
702
|
+
var operation, tableId, rowId, task, startedAt, result, _getErrorCode;
|
|
703
|
+
return _regeneratorRuntime().wrap(function _callee15$(_context15) {
|
|
704
|
+
while (1) switch (_context15.prev = _context15.next) {
|
|
705
|
+
case 0:
|
|
706
|
+
operation = _ref6.operation, tableId = _ref6.tableId, rowId = _ref6.rowId, task = _ref6.task;
|
|
707
|
+
startedAt = Date.now();
|
|
708
|
+
_context15.prev = 2;
|
|
709
|
+
_context15.next = 5;
|
|
710
|
+
return task();
|
|
711
|
+
case 5:
|
|
712
|
+
result = _context15.sent;
|
|
713
|
+
scheduleMutationAudit({
|
|
714
|
+
operation: operation,
|
|
715
|
+
tableId: tableId,
|
|
716
|
+
rowId: rowId,
|
|
717
|
+
outcome: 'success',
|
|
718
|
+
durationMs: Date.now() - startedAt
|
|
719
|
+
});
|
|
720
|
+
return _context15.abrupt("return", result);
|
|
721
|
+
case 10:
|
|
722
|
+
_context15.prev = 10;
|
|
723
|
+
_context15.t0 = _context15["catch"](2);
|
|
724
|
+
scheduleMutationAudit({
|
|
725
|
+
operation: operation,
|
|
726
|
+
tableId: tableId,
|
|
727
|
+
rowId: rowId,
|
|
728
|
+
outcome: 'failure',
|
|
729
|
+
durationMs: Date.now() - startedAt,
|
|
730
|
+
errorCode: (_getErrorCode = getErrorCode(_context15.t0)) !== null && _getErrorCode !== void 0 ? _getErrorCode : undefined
|
|
731
|
+
});
|
|
732
|
+
throw _context15.t0;
|
|
733
|
+
case 14:
|
|
734
|
+
case "end":
|
|
735
|
+
return _context15.stop();
|
|
736
|
+
}
|
|
737
|
+
}, _callee15, null, [[2, 10]]);
|
|
738
|
+
}));
|
|
739
|
+
return _observeMutation.apply(this, arguments);
|
|
740
|
+
}
|
|
741
|
+
function listRows(_x5) {
|
|
742
|
+
return _listRows.apply(this, arguments);
|
|
743
|
+
}
|
|
744
|
+
function _listRows() {
|
|
745
|
+
_listRows = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(_ref7) {
|
|
746
|
+
var tableId, _ref7$queries, queries, _ref7$limit, limit, cursor, _yield$getClients, databaseId;
|
|
747
|
+
return _regeneratorRuntime().wrap(function _callee16$(_context16) {
|
|
748
|
+
while (1) switch (_context16.prev = _context16.next) {
|
|
749
|
+
case 0:
|
|
750
|
+
tableId = _ref7.tableId, _ref7$queries = _ref7.queries, queries = _ref7$queries === void 0 ? [] : _ref7$queries, _ref7$limit = _ref7.limit, limit = _ref7$limit === void 0 ? 20 : _ref7$limit, cursor = _ref7.cursor;
|
|
751
|
+
if (!(!Number.isInteger(limit) || limit < 1 || limit > 20)) {
|
|
752
|
+
_context16.next = 3;
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
throw new RangeError('listRows limit must be an integer from 1 to 20');
|
|
756
|
+
case 3:
|
|
757
|
+
_context16.next = 5;
|
|
758
|
+
return getClients();
|
|
759
|
+
case 5:
|
|
760
|
+
_yield$getClients = _context16.sent;
|
|
761
|
+
databaseId = _yield$getClients.databaseId;
|
|
762
|
+
return _context16.abrupt("return", withSessionRetry(function () {
|
|
763
|
+
return tablesDB.listRows({
|
|
764
|
+
databaseId: databaseId,
|
|
765
|
+
tableId: tableId,
|
|
766
|
+
queries: [Query.orderDesc('$createdAt')].concat(_toConsumableArray(queries), [Query.limit(limit)], _toConsumableArray(cursor ? [Query.cursorAfter(cursor)] : []))
|
|
767
|
+
});
|
|
768
|
+
}));
|
|
769
|
+
case 8:
|
|
770
|
+
case "end":
|
|
771
|
+
return _context16.stop();
|
|
772
|
+
}
|
|
773
|
+
}, _callee16);
|
|
774
|
+
}));
|
|
775
|
+
return _listRows.apply(this, arguments);
|
|
776
|
+
}
|
|
777
|
+
function getRow(_x6) {
|
|
778
|
+
return _getRow.apply(this, arguments);
|
|
779
|
+
}
|
|
780
|
+
function _getRow() {
|
|
781
|
+
_getRow = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(_ref8) {
|
|
782
|
+
var tableId, rowId, _yield$getClients2, databaseId;
|
|
783
|
+
return _regeneratorRuntime().wrap(function _callee17$(_context17) {
|
|
784
|
+
while (1) switch (_context17.prev = _context17.next) {
|
|
785
|
+
case 0:
|
|
786
|
+
tableId = _ref8.tableId, rowId = _ref8.rowId;
|
|
787
|
+
_context17.next = 3;
|
|
788
|
+
return getClients();
|
|
789
|
+
case 3:
|
|
790
|
+
_yield$getClients2 = _context17.sent;
|
|
791
|
+
databaseId = _yield$getClients2.databaseId;
|
|
792
|
+
return _context17.abrupt("return", withSessionRetry(function () {
|
|
793
|
+
return tablesDB.getRow({
|
|
794
|
+
databaseId: databaseId,
|
|
795
|
+
tableId: tableId,
|
|
796
|
+
rowId: rowId
|
|
797
|
+
});
|
|
798
|
+
}));
|
|
799
|
+
case 6:
|
|
800
|
+
case "end":
|
|
801
|
+
return _context17.stop();
|
|
802
|
+
}
|
|
803
|
+
}, _callee17);
|
|
804
|
+
}));
|
|
805
|
+
return _getRow.apply(this, arguments);
|
|
806
|
+
}
|
|
807
|
+
function createRow(_x7) {
|
|
808
|
+
return _createRow.apply(this, arguments);
|
|
809
|
+
}
|
|
810
|
+
function _createRow() {
|
|
811
|
+
_createRow = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(_ref9) {
|
|
812
|
+
var tableId, data, _ref9$rowId, rowId, _yield$getClients3, databaseId;
|
|
813
|
+
return _regeneratorRuntime().wrap(function _callee18$(_context18) {
|
|
814
|
+
while (1) switch (_context18.prev = _context18.next) {
|
|
815
|
+
case 0:
|
|
816
|
+
tableId = _ref9.tableId, data = _ref9.data, _ref9$rowId = _ref9.rowId, rowId = _ref9$rowId === void 0 ? ID.unique() : _ref9$rowId;
|
|
817
|
+
_context18.next = 3;
|
|
818
|
+
return getClients();
|
|
819
|
+
case 3:
|
|
820
|
+
_yield$getClients3 = _context18.sent;
|
|
821
|
+
databaseId = _yield$getClients3.databaseId;
|
|
822
|
+
return _context18.abrupt("return", observeMutation({
|
|
823
|
+
operation: 'create',
|
|
824
|
+
tableId: tableId,
|
|
825
|
+
rowId: rowId,
|
|
826
|
+
task: function task() {
|
|
827
|
+
return withSessionRetry(function () {
|
|
828
|
+
var currentUserId = getCurrentUser().$id;
|
|
829
|
+
var owner = Role.user(currentUserId);
|
|
830
|
+
return tablesDB.createRow({
|
|
831
|
+
databaseId: databaseId,
|
|
832
|
+
tableId: tableId,
|
|
833
|
+
rowId: rowId,
|
|
834
|
+
data: _objectSpread(_objectSpread({}, sanitizeBusinessRowData(data)), {}, {
|
|
835
|
+
creatorId: currentUserId,
|
|
836
|
+
modifierId: currentUserId
|
|
837
|
+
}),
|
|
838
|
+
permissions: [Permission.read(owner), Permission.update(owner), Permission.delete(owner)]
|
|
839
|
+
});
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
}));
|
|
843
|
+
case 6:
|
|
844
|
+
case "end":
|
|
845
|
+
return _context18.stop();
|
|
846
|
+
}
|
|
847
|
+
}, _callee18);
|
|
848
|
+
}));
|
|
849
|
+
return _createRow.apply(this, arguments);
|
|
850
|
+
}
|
|
851
|
+
function updateRow(_x8) {
|
|
852
|
+
return _updateRow.apply(this, arguments);
|
|
853
|
+
}
|
|
854
|
+
function _updateRow() {
|
|
855
|
+
_updateRow = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19(_ref10) {
|
|
856
|
+
var tableId, rowId, data, _yield$getClients4, databaseId;
|
|
857
|
+
return _regeneratorRuntime().wrap(function _callee19$(_context19) {
|
|
858
|
+
while (1) switch (_context19.prev = _context19.next) {
|
|
859
|
+
case 0:
|
|
860
|
+
tableId = _ref10.tableId, rowId = _ref10.rowId, data = _ref10.data;
|
|
861
|
+
_context19.next = 3;
|
|
862
|
+
return getClients();
|
|
863
|
+
case 3:
|
|
864
|
+
_yield$getClients4 = _context19.sent;
|
|
865
|
+
databaseId = _yield$getClients4.databaseId;
|
|
866
|
+
return _context19.abrupt("return", observeMutation({
|
|
867
|
+
operation: 'update',
|
|
868
|
+
tableId: tableId,
|
|
869
|
+
rowId: rowId,
|
|
870
|
+
task: function task() {
|
|
871
|
+
return withSessionRetry(function () {
|
|
872
|
+
return tablesDB.updateRow({
|
|
873
|
+
databaseId: databaseId,
|
|
874
|
+
tableId: tableId,
|
|
875
|
+
rowId: rowId,
|
|
876
|
+
data: _objectSpread(_objectSpread({}, sanitizeBusinessRowData(data)), {}, {
|
|
877
|
+
modifierId: getCurrentUser().$id
|
|
878
|
+
})
|
|
879
|
+
});
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
}));
|
|
883
|
+
case 6:
|
|
884
|
+
case "end":
|
|
885
|
+
return _context19.stop();
|
|
886
|
+
}
|
|
887
|
+
}, _callee19);
|
|
888
|
+
}));
|
|
889
|
+
return _updateRow.apply(this, arguments);
|
|
890
|
+
}
|
|
891
|
+
function deleteRow(_x9) {
|
|
892
|
+
return _deleteRow.apply(this, arguments);
|
|
893
|
+
}
|
|
894
|
+
function _deleteRow() {
|
|
895
|
+
_deleteRow = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20(_ref11) {
|
|
896
|
+
var tableId, rowId, _yield$getClients5, databaseId;
|
|
897
|
+
return _regeneratorRuntime().wrap(function _callee20$(_context20) {
|
|
898
|
+
while (1) switch (_context20.prev = _context20.next) {
|
|
899
|
+
case 0:
|
|
900
|
+
tableId = _ref11.tableId, rowId = _ref11.rowId;
|
|
901
|
+
_context20.next = 3;
|
|
902
|
+
return getClients();
|
|
903
|
+
case 3:
|
|
904
|
+
_yield$getClients5 = _context20.sent;
|
|
905
|
+
databaseId = _yield$getClients5.databaseId;
|
|
906
|
+
return _context20.abrupt("return", observeMutation({
|
|
907
|
+
operation: 'delete',
|
|
908
|
+
tableId: tableId,
|
|
909
|
+
rowId: rowId,
|
|
910
|
+
task: function task() {
|
|
911
|
+
return withSessionRetry(function () {
|
|
912
|
+
return tablesDB.deleteRow({
|
|
913
|
+
databaseId: databaseId,
|
|
914
|
+
tableId: tableId,
|
|
915
|
+
rowId: rowId
|
|
916
|
+
});
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
}));
|
|
920
|
+
case 6:
|
|
921
|
+
case "end":
|
|
922
|
+
return _context20.stop();
|
|
923
|
+
}
|
|
924
|
+
}, _callee20);
|
|
925
|
+
}));
|
|
926
|
+
return _deleteRow.apply(this, arguments);
|
|
927
|
+
}
|
|
928
|
+
return {
|
|
929
|
+
init: init,
|
|
930
|
+
getClients: getClients,
|
|
931
|
+
getCurrentUser: getCurrentUser,
|
|
932
|
+
refreshSession: refreshSession,
|
|
933
|
+
listRows: listRows,
|
|
934
|
+
getRow: getRow,
|
|
935
|
+
createRow: createRow,
|
|
936
|
+
updateRow: updateRow,
|
|
937
|
+
deleteRow: deleteRow
|
|
938
|
+
};
|
|
939
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export type QmuseEnvironment = 'dev' | 'prod';
|
|
2
|
+
export interface QmuseRuntimeContext {
|
|
3
|
+
appId: string;
|
|
4
|
+
appVersionId: string;
|
|
5
|
+
env: QmuseEnvironment;
|
|
6
|
+
domain: string;
|
|
7
|
+
}
|
|
8
|
+
export interface AppwriteRuntimeConfig {
|
|
9
|
+
endpoint: string;
|
|
10
|
+
projectId: string;
|
|
11
|
+
databaseId: string;
|
|
12
|
+
authTokenEndpoint: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AppwriteUser {
|
|
15
|
+
$id: string;
|
|
16
|
+
}
|
|
17
|
+
export interface AppwriteSession {
|
|
18
|
+
provider: string;
|
|
19
|
+
}
|
|
20
|
+
export interface AppwriteError {
|
|
21
|
+
code?: number | string;
|
|
22
|
+
}
|
|
23
|
+
export type BusinessRowData = Record<string, unknown>;
|
|
24
|
+
export interface AppwriteClientLike {
|
|
25
|
+
setEndpoint(endpoint: string): AppwriteClientLike;
|
|
26
|
+
setProject(projectId: string): AppwriteClientLike;
|
|
27
|
+
}
|
|
28
|
+
export interface AppwriteAccountLike {
|
|
29
|
+
get(): Promise<AppwriteUser>;
|
|
30
|
+
getSession(input: {
|
|
31
|
+
sessionId: 'current';
|
|
32
|
+
}): Promise<AppwriteSession>;
|
|
33
|
+
deleteSession(input: {
|
|
34
|
+
sessionId: 'current';
|
|
35
|
+
}): Promise<void>;
|
|
36
|
+
createAnonymousSession(): Promise<unknown>;
|
|
37
|
+
createSession(input: {
|
|
38
|
+
userId: string;
|
|
39
|
+
secret: string;
|
|
40
|
+
}): Promise<unknown>;
|
|
41
|
+
}
|
|
42
|
+
export interface AppwriteTablesDBLike {
|
|
43
|
+
listRows(input: {
|
|
44
|
+
databaseId: string;
|
|
45
|
+
tableId: string;
|
|
46
|
+
queries: string[];
|
|
47
|
+
}): Promise<unknown>;
|
|
48
|
+
getRow(input: {
|
|
49
|
+
databaseId: string;
|
|
50
|
+
tableId: string;
|
|
51
|
+
rowId: string;
|
|
52
|
+
}): Promise<unknown>;
|
|
53
|
+
createRow(input: {
|
|
54
|
+
databaseId: string;
|
|
55
|
+
tableId: string;
|
|
56
|
+
rowId: string;
|
|
57
|
+
data: BusinessRowData;
|
|
58
|
+
permissions: string[];
|
|
59
|
+
}): Promise<unknown>;
|
|
60
|
+
updateRow(input: {
|
|
61
|
+
databaseId: string;
|
|
62
|
+
tableId: string;
|
|
63
|
+
rowId: string;
|
|
64
|
+
data: BusinessRowData;
|
|
65
|
+
}): Promise<unknown>;
|
|
66
|
+
deleteRow(input: {
|
|
67
|
+
databaseId: string;
|
|
68
|
+
tableId: string;
|
|
69
|
+
rowId: string;
|
|
70
|
+
}): Promise<unknown>;
|
|
71
|
+
}
|
|
72
|
+
export interface QmuseAppwriteRuntimeOptions {
|
|
73
|
+
client: AppwriteClientLike;
|
|
74
|
+
account: AppwriteAccountLike;
|
|
75
|
+
tablesDB: AppwriteTablesDBLike;
|
|
76
|
+
getMuseRuntime(): QmuseRuntimeContext;
|
|
77
|
+
getQmuseLoginUserId(): string | null;
|
|
78
|
+
fetch?: typeof globalThis.fetch;
|
|
79
|
+
}
|
|
80
|
+
export interface QmuseAppwriteClients {
|
|
81
|
+
client: AppwriteClientLike;
|
|
82
|
+
account: AppwriteAccountLike;
|
|
83
|
+
tablesDB: AppwriteTablesDBLike;
|
|
84
|
+
databaseId: string;
|
|
85
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qmuse/appwrite-runtime-sdk",
|
|
3
|
+
"version": "0.0.1-dev.1",
|
|
4
|
+
"description": "QMuse Appwrite browser runtime SDK",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://code.alipay.com/yfd-air/appwrite-runtime-sdk"
|
|
8
|
+
},
|
|
9
|
+
"author": "fuxiao.yp",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"module": "dist/index.js",
|
|
18
|
+
"typings": "dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"icons",
|
|
22
|
+
"LEGAL.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "father build",
|
|
26
|
+
"build:watch": "father build --watch",
|
|
27
|
+
"ci": "npm run lint && jest --coverage --passWithNoTests",
|
|
28
|
+
"cov": "jest --coverage",
|
|
29
|
+
"doctor": "father doctor",
|
|
30
|
+
"lint": "eslint . --ext .js,.jsx,.ts,.tsx && stylelint \"src/**/*.{css,less,scss,sass}\" --allow-empty-input",
|
|
31
|
+
"prepare": "husky install",
|
|
32
|
+
"prepublishOnly": "npm run build",
|
|
33
|
+
"publish:dev": "bash scripts/publish-dev.sh",
|
|
34
|
+
"test": "jest"
|
|
35
|
+
},
|
|
36
|
+
"commitlint": {
|
|
37
|
+
"extends": [
|
|
38
|
+
"@commitlint/config-conventional"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
"lint-staged": {
|
|
42
|
+
"*.{md,json}": [
|
|
43
|
+
"prettier --cache --write --no-error-on-unmatched-pattern"
|
|
44
|
+
],
|
|
45
|
+
"*.{js,jsx}": [
|
|
46
|
+
"eslint --fix",
|
|
47
|
+
"prettier --cache --write"
|
|
48
|
+
],
|
|
49
|
+
"*.{css,less}": [
|
|
50
|
+
"stylelint --fix",
|
|
51
|
+
"prettier --cache --write"
|
|
52
|
+
],
|
|
53
|
+
"*.{ts,tsx}": [
|
|
54
|
+
"eslint --fix",
|
|
55
|
+
"prettier --cache --parser=typescript --write"
|
|
56
|
+
]
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@babel/runtime": "^7.18.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@ali/ci": "^4.26.0",
|
|
63
|
+
"@umijs/lint": "^4.0.34",
|
|
64
|
+
"@umijs/test": "^4.0.34",
|
|
65
|
+
"@commitlint/cli": "^17.3.0",
|
|
66
|
+
"@commitlint/config-conventional": "^17.3.0",
|
|
67
|
+
"@types/jest": "^29.4.0",
|
|
68
|
+
"appwrite": "24.1.1",
|
|
69
|
+
"eslint": "8.15.0",
|
|
70
|
+
"father": "^4.3.8",
|
|
71
|
+
"husky": "^8.0.0",
|
|
72
|
+
"jest": "^29.4.3",
|
|
73
|
+
"jest-environment-jsdom": "^29.4.3",
|
|
74
|
+
"lint-staged": "^13.0.0",
|
|
75
|
+
"prettier": "^2.0.0",
|
|
76
|
+
"stylelint": "14.8.2"
|
|
77
|
+
},
|
|
78
|
+
"peerDependencies": {
|
|
79
|
+
"appwrite": "24.1.1"
|
|
80
|
+
},
|
|
81
|
+
"engines": {
|
|
82
|
+
"install-node": "20"
|
|
83
|
+
},
|
|
84
|
+
"publishConfig": {
|
|
85
|
+
"registry": "https://registry.npmjs.org/"
|
|
86
|
+
},
|
|
87
|
+
"ci": {
|
|
88
|
+
"type": "aci"
|
|
89
|
+
},
|
|
90
|
+
"libType": "UTIL",
|
|
91
|
+
"tnpm": {
|
|
92
|
+
"mode": "npm"
|
|
93
|
+
},
|
|
94
|
+
"yuyanId": "180020010101342753"
|
|
95
|
+
}
|