@orjok/commons 1.0.2
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/README.md +204 -0
- package/dist/amplify.cjs +129 -0
- package/dist/amplify.cjs.map +1 -0
- package/dist/amplify.d.cts +40 -0
- package/dist/amplify.d.ts +40 -0
- package/dist/amplify.js +129 -0
- package/dist/amplify.js.map +1 -0
- package/dist/client-BaOC79Nc.d.cts +775 -0
- package/dist/client-Bm9aiyTx.d.ts +775 -0
- package/dist/hooks.cjs +16 -0
- package/dist/hooks.cjs.map +1 -0
- package/dist/hooks.d.cts +8 -0
- package/dist/hooks.d.ts +8 -0
- package/dist/hooks.js +16 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.cjs +1723 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +1723 -0
- package/dist/index.js.map +1 -0
- package/dist/storage-DLqdJgQt.d.cts +72 -0
- package/dist/storage-DLqdJgQt.d.ts +72 -0
- package/dist/testing.cjs +97 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +52 -0
- package/dist/testing.d.ts +52 -0
- package/dist/testing.js +97 -0
- package/dist/testing.js.map +1 -0
- package/package.json +104 -0
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# @orjok/commons
|
|
2
|
+
|
|
3
|
+
Shared business logic for the Orjok platform. Works with React (web) and React Native.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @orjok/commons
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Entry Points
|
|
12
|
+
|
|
13
|
+
| Import path | Purpose | Required peer deps |
|
|
14
|
+
| --------------------------------- | ------------------------------------- | -------------------------------- |
|
|
15
|
+
| `@orjok/commons` | Client, services, types, logic, utils | — |
|
|
16
|
+
| `@orjok/commons/adapters/amplify` | AWS Amplify provider implementations | `aws-amplify` |
|
|
17
|
+
| `@orjok/commons/hooks` | React context & `useOrjokClient` hook | `react`, `@tanstack/react-query` |
|
|
18
|
+
| `@orjok/commons/testing` | Mock providers for unit tests | — |
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### 1. Create a client
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { createOrjokClient } from "@orjok/commons";
|
|
26
|
+
import {
|
|
27
|
+
AmplifyAuthProvider,
|
|
28
|
+
AmplifyNetworkProvider,
|
|
29
|
+
AmplifyStorageProvider,
|
|
30
|
+
} from "@orjok/commons/adapters/amplify";
|
|
31
|
+
|
|
32
|
+
const client = createOrjokClient({
|
|
33
|
+
authProvider: new AmplifyAuthProvider(),
|
|
34
|
+
networkProvider: new AmplifyNetworkProvider(),
|
|
35
|
+
storageProvider: new AmplifyStorageProvider(), // optional
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Use services directly
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
const user = await client.users.get("user-id");
|
|
43
|
+
const questions = await client.questions.listBySubject({ id: "physics" });
|
|
44
|
+
const contest = await client.contests.get("contest-id");
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 3. Use with React
|
|
48
|
+
|
|
49
|
+
Wrap your app with `OrjokProvider`, then use `useOrjokClient()` in any component:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { OrjokProvider } from "@orjok/commons/hooks";
|
|
53
|
+
|
|
54
|
+
function App() {
|
|
55
|
+
return (
|
|
56
|
+
<OrjokProvider value={client}>
|
|
57
|
+
<MyApp />
|
|
58
|
+
</OrjokProvider>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { useOrjokClient } from "@orjok/commons/hooks";
|
|
65
|
+
|
|
66
|
+
function UserProfile({ userId }: { userId: string }) {
|
|
67
|
+
const client = useOrjokClient();
|
|
68
|
+
// use client.users, client.questions, etc.
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Available Services
|
|
73
|
+
|
|
74
|
+
| Service | Access via | Examples |
|
|
75
|
+
| -------------- | ------------------- | --------------------------------------------------------- |
|
|
76
|
+
| **Auth** | `client.auth` | `signIn`, `signUp`, `signOut`, `fetchUserAttributes` |
|
|
77
|
+
| **Users** | `client.users` | `get`, `updateAvatar`, `getQuestions`, `getPacks` |
|
|
78
|
+
| **Questions** | `client.questions` | `create`, `get`, `vote`, `listBySubject`, `addTags` |
|
|
79
|
+
| **Packs** | `client.packs` | `create`, `get`, `submitExam`, `listGroups` |
|
|
80
|
+
| **Contests** | `client.contests` | `create`, `participate`, `selectOption`, `getLeaderboard` |
|
|
81
|
+
| **Courses** | `client.courses` | `create`, `enroll`, `createItem`, `createLiveExam` |
|
|
82
|
+
| **Curriculum** | `client.curriculum` | `createSubject`, `getChapters`, `getTopics` |
|
|
83
|
+
| **Media** | `client.media` | `uploadImage`, `deleteImage`, `getSignedURL` |
|
|
84
|
+
| **Progress** | `client.progress` | `track`, `getSubjectProgress`, `getMistakenQuestions` |
|
|
85
|
+
| **AI** | `client.ai` | `evaluateWrittenAnswer` |
|
|
86
|
+
| **News** | `client.news` | `create`, `list`, `get` |
|
|
87
|
+
|
|
88
|
+
## Logic Utilities
|
|
89
|
+
|
|
90
|
+
Pure functions with zero dependencies — usable anywhere:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import {
|
|
94
|
+
getEloTier,
|
|
95
|
+
computeRatingDelta,
|
|
96
|
+
calculateExamTime,
|
|
97
|
+
scoreExam,
|
|
98
|
+
parseTextQuestions,
|
|
99
|
+
aggregateSubjectMetrics,
|
|
100
|
+
} from "@orjok/commons";
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Media URL Resolution
|
|
104
|
+
|
|
105
|
+
Models return media fields (`imageUrl`, `avatarUrl`, etc.) as raw Storage keys. Use the built-in resolver instead of handling this yourself:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import {
|
|
109
|
+
resolveMediaUrl,
|
|
110
|
+
resolveMediaUrls,
|
|
111
|
+
MediaUrlCache,
|
|
112
|
+
isStorageKey,
|
|
113
|
+
} from "@orjok/commons";
|
|
114
|
+
|
|
115
|
+
// Single field
|
|
116
|
+
const url = await resolveMediaUrl(question.imageUrl, client.storage);
|
|
117
|
+
|
|
118
|
+
// Batch — resolve multiple fields on a model in one call
|
|
119
|
+
const resolved = await resolveMediaUrls(question, ["imageUrl"], client.storage);
|
|
120
|
+
// resolved.imageUrl is now a renderable URL (or null)
|
|
121
|
+
|
|
122
|
+
// With caching (recommended) — avoids re-resolving the same key within TTL
|
|
123
|
+
const cache = new MediaUrlCache(); // default 50min TTL for signed URLs
|
|
124
|
+
const url1 = await resolveMediaUrl(key, client.storage, cache);
|
|
125
|
+
const url2 = await resolveMediaUrl(key, client.storage, cache); // cache hit, no network call
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Behavior:**
|
|
129
|
+
|
|
130
|
+
- Absolute URLs (`https://...`) are returned as-is — no StorageProvider call.
|
|
131
|
+
- `null`, `undefined`, or empty string → returns `null`.
|
|
132
|
+
- No StorageProvider configured → returns the raw key with a one-time console warning (no crash).
|
|
133
|
+
|
|
134
|
+
## General Utilities
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import {
|
|
138
|
+
shuffle,
|
|
139
|
+
engToBanglaNumber,
|
|
140
|
+
formatCurriculumName,
|
|
141
|
+
getRelativeTime,
|
|
142
|
+
processLatexText,
|
|
143
|
+
} from "@orjok/commons";
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Custom Providers
|
|
147
|
+
|
|
148
|
+
Implement the provider interfaces to use a different backend:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import type { AuthProvider, NetworkProvider, StorageProvider } from "@orjok/commons";
|
|
152
|
+
|
|
153
|
+
class MyAuthProvider implements AuthProvider {
|
|
154
|
+
// implement signIn, signUp, signOut, etc.
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class MyNetworkProvider implements NetworkProvider {
|
|
158
|
+
async query<T>(name: string, query: string, variables?: Record<string, unknown>) { ... }
|
|
159
|
+
async mutate<T>(name: string, mutation: string, variables?: Record<string, unknown>) { ... }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const client = createOrjokClient({
|
|
163
|
+
authProvider: new MyAuthProvider(),
|
|
164
|
+
networkProvider: new MyNetworkProvider(),
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Testing
|
|
169
|
+
|
|
170
|
+
Use the mock providers to test code that depends on the client:
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import { MockNetworkProvider, MockAuthProvider } from "@orjok/commons/testing";
|
|
174
|
+
import { UserService } from "@orjok/commons";
|
|
175
|
+
|
|
176
|
+
const network = new MockNetworkProvider();
|
|
177
|
+
const service = new UserService(network);
|
|
178
|
+
|
|
179
|
+
network.setResponse("getUser", { getUser: { id: "u1", fullName: "Test" } });
|
|
180
|
+
|
|
181
|
+
const user = await service.get("u1");
|
|
182
|
+
expect(user.fullName).toBe("Test");
|
|
183
|
+
expect(network.calls).toHaveLength(1);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Schema Validation
|
|
187
|
+
|
|
188
|
+
The test suite automatically validates that every GraphQL operation name used in service files matches the live AppSync schema. This prevents field-name drift (e.g. using `listQuestionObjectsBySubjectId…` when AppSync expects the singular `listQuestionObjectBySubjectId…`).
|
|
189
|
+
|
|
190
|
+
The `sync-schema` script parses `amplify/data/resource.ts` and generates `src/graphql/schema-manifest.ts` containing:
|
|
191
|
+
|
|
192
|
+
- `VALID_QUERIES` / `VALID_MUTATIONS` — every operation the schema exposes
|
|
193
|
+
- `MODEL_FIELD_TYPES` / `GSI_FIELD_TYPES` — field metadata for each model
|
|
194
|
+
|
|
195
|
+
Run `npm run sync-schema` after any schema change, then `npm test` — the `schema-validation.test.ts` suite will fail with a "did you mean?" hint for any operation that doesn't exist.
|
|
196
|
+
|
|
197
|
+
## Development
|
|
198
|
+
|
|
199
|
+
```sh
|
|
200
|
+
npm run sync-schema # regenerate schema manifest from amplify/data/resource.ts
|
|
201
|
+
npm test # run tests (includes schema validation)
|
|
202
|
+
npm run typecheck # type-check without emitting
|
|
203
|
+
npm run build # bundle with tsup
|
|
204
|
+
```
|
package/dist/amplify.cjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/adapters/amplify/auth-provider.ts
|
|
2
|
+
var AmplifyAuthProvider = class {
|
|
3
|
+
async signIn(username, password) {
|
|
4
|
+
const { signIn } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
5
|
+
const result = await signIn({ username, password });
|
|
6
|
+
return { isSignedIn: result.isSignedIn };
|
|
7
|
+
}
|
|
8
|
+
async signUp(username, password, attributes) {
|
|
9
|
+
const { signUp } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
10
|
+
const result = await signUp({
|
|
11
|
+
username,
|
|
12
|
+
password,
|
|
13
|
+
options: { userAttributes: attributes }
|
|
14
|
+
});
|
|
15
|
+
return { isSignUpComplete: result.isSignUpComplete, userId: result.userId };
|
|
16
|
+
}
|
|
17
|
+
async signOut() {
|
|
18
|
+
const { signOut } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
19
|
+
await signOut();
|
|
20
|
+
}
|
|
21
|
+
async signInWithRedirect(provider) {
|
|
22
|
+
const { signInWithRedirect } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
23
|
+
await signInWithRedirect({ provider: { custom: provider } });
|
|
24
|
+
}
|
|
25
|
+
async fetchUserAttributes() {
|
|
26
|
+
const { fetchUserAttributes } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
27
|
+
const attrs = await fetchUserAttributes();
|
|
28
|
+
return attrs;
|
|
29
|
+
}
|
|
30
|
+
async updateUserAttributes(attributes) {
|
|
31
|
+
const { updateUserAttributes } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
32
|
+
const userAttributes = {};
|
|
33
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
34
|
+
userAttributes[key] = value;
|
|
35
|
+
}
|
|
36
|
+
await updateUserAttributes({ userAttributes });
|
|
37
|
+
}
|
|
38
|
+
async fetchAuthSession() {
|
|
39
|
+
const { fetchAuthSession } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
40
|
+
const session = await fetchAuthSession();
|
|
41
|
+
return session;
|
|
42
|
+
}
|
|
43
|
+
async confirmSignUp(username, code) {
|
|
44
|
+
const { confirmSignUp } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
45
|
+
const result = await confirmSignUp({ username, confirmationCode: code });
|
|
46
|
+
return { isSignUpComplete: result.isSignUpComplete };
|
|
47
|
+
}
|
|
48
|
+
async resendSignUpCode(username) {
|
|
49
|
+
const { resendSignUpCode } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
50
|
+
await resendSignUpCode({ username });
|
|
51
|
+
}
|
|
52
|
+
async resetPassword(username) {
|
|
53
|
+
const { resetPassword } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
54
|
+
await resetPassword({ username });
|
|
55
|
+
}
|
|
56
|
+
async confirmResetPassword(username, code, newPassword) {
|
|
57
|
+
const { confirmResetPassword } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
58
|
+
await confirmResetPassword({ username, confirmationCode: code, newPassword });
|
|
59
|
+
}
|
|
60
|
+
async getCurrentUserId() {
|
|
61
|
+
try {
|
|
62
|
+
const { getCurrentUser } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
63
|
+
const user = await getCurrentUser();
|
|
64
|
+
return user.userId;
|
|
65
|
+
} catch (e) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/adapters/amplify/network-provider.ts
|
|
72
|
+
var AmplifyNetworkProvider = class {
|
|
73
|
+
async getAuthMode() {
|
|
74
|
+
try {
|
|
75
|
+
const { fetchAuthSession } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/auth")));
|
|
76
|
+
const session = await fetchAuthSession();
|
|
77
|
+
if (_optionalChain([session, 'access', _ => _.tokens, 'optionalAccess', _2 => _2.accessToken])) return "userPool";
|
|
78
|
+
} catch (e2) {
|
|
79
|
+
}
|
|
80
|
+
return "apiKey";
|
|
81
|
+
}
|
|
82
|
+
async query(query, variables) {
|
|
83
|
+
const { generateClient } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/api")));
|
|
84
|
+
const client = generateClient();
|
|
85
|
+
const authMode = await this.getAuthMode();
|
|
86
|
+
const result = await client.graphql({ query, variables: _nullishCoalesce(variables, () => ( {})), authMode });
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
async mutate(mutation, variables) {
|
|
90
|
+
const { generateClient } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/api")));
|
|
91
|
+
const client = generateClient();
|
|
92
|
+
const authMode = await this.getAuthMode();
|
|
93
|
+
const result = await client.graphql({ query: mutation, variables: _nullishCoalesce(variables, () => ( {})), authMode });
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/adapters/amplify/storage-provider.ts
|
|
99
|
+
var AmplifyStorageProvider = class {
|
|
100
|
+
async getFileUrl(path) {
|
|
101
|
+
const { getUrl } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/storage")));
|
|
102
|
+
const result = await getUrl({ path });
|
|
103
|
+
return { url: result.url.toString() };
|
|
104
|
+
}
|
|
105
|
+
async uploadFile(path, data, options) {
|
|
106
|
+
const { uploadData } = await Promise.resolve().then(() => _interopRequireWildcard(require("aws-amplify/storage")));
|
|
107
|
+
const result = uploadData({
|
|
108
|
+
path,
|
|
109
|
+
data,
|
|
110
|
+
options: {
|
|
111
|
+
contentType: _optionalChain([options, 'optionalAccess', _3 => _3.contentType]),
|
|
112
|
+
onProgress: _optionalChain([options, 'optionalAccess', _4 => _4.onProgress]) ? (event) => {
|
|
113
|
+
options.onProgress({
|
|
114
|
+
loaded: _nullishCoalesce(event.loaded, () => ( 0)),
|
|
115
|
+
total: _nullishCoalesce(event.total, () => ( 0))
|
|
116
|
+
});
|
|
117
|
+
} : void 0
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
await result.result;
|
|
121
|
+
return { path };
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
exports.AmplifyAuthProvider = AmplifyAuthProvider; exports.AmplifyNetworkProvider = AmplifyNetworkProvider; exports.AmplifyStorageProvider = AmplifyStorageProvider;
|
|
129
|
+
//# sourceMappingURL=amplify.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/orjok/orjok/packages/commons/dist/amplify.cjs","../src/adapters/amplify/auth-provider.ts","../src/adapters/amplify/network-provider.ts","../src/adapters/amplify/storage-provider.ts"],"names":[],"mappings":"AAAA;ACEO,IAAM,oBAAA,EAAN,MAAkD;AAAA,EACvD,MAAM,MAAA,CAAO,QAAA,EAAkB,QAAA,EAAkB;AAC/C,IAAA,MAAM,EAAE,OAAO,EAAA,EAAI,MAAM,4DAAA,CAAO,kBAAkB,GAAA;AAClD,IAAA,MAAM,OAAA,EAAS,MAAM,MAAA,CAAO,EAAE,QAAA,EAAU,SAAS,CAAC,CAAA;AAClD,IAAA,OAAO,EAAE,UAAA,EAAY,MAAA,CAAO,WAAW,CAAA;AAAA,EACzC;AAAA,EAEA,MAAM,MAAA,CAAO,QAAA,EAAkB,QAAA,EAAkB,UAAA,EAAoC;AACnF,IAAA,MAAM,EAAE,OAAO,EAAA,EAAI,MAAM,4DAAA,CAAO,kBAAkB,GAAA;AAClD,IAAA,MAAM,OAAA,EAAS,MAAM,MAAA,CAAO;AAAA,MAC1B,QAAA;AAAA,MACA,QAAA;AAAA,MACA,OAAA,EAAS,EAAE,cAAA,EAAgB,WAAW;AAAA,IACxC,CAAC,CAAA;AACD,IAAA,OAAO,EAAE,gBAAA,EAAkB,MAAA,CAAO,gBAAA,EAAkB,MAAA,EAAQ,MAAA,CAAO,OAAO,CAAA;AAAA,EAC5E;AAAA,EAEA,MAAM,OAAA,CAAA,EAAU;AACd,IAAA,MAAM,EAAE,QAAQ,EAAA,EAAI,MAAM,4DAAA,CAAO,kBAAkB,GAAA;AACnD,IAAA,MAAM,OAAA,CAAQ,CAAA;AAAA,EAChB;AAAA,EAEA,MAAM,kBAAA,CAAmB,QAAA,EAAkB;AACzC,IAAA,MAAM,EAAE,mBAAmB,EAAA,EAAI,MAAM,4DAAA,CAAO,kBAAkB,GAAA;AAC9D,IAAA,MAAM,kBAAA,CAA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O,GAAA,CAAI,QAAA,CAAS,EAAE,CAAA;AAAA,EACtC;AAAA,EAEA,MAAM,UAAA,CACJ,IAAA,EACA,IAAA,EACA,OAAA,EAC2B;AAC3B,IAAA,MAAM,EAAE,WAAW,EAAA,EAAI,MAAM,4DAAA,CAAO,qBAAqB,GAAA;AACzD,IAAA,MAAM,OAAA,EAAS,UAAA,CAAW;AAAA,MACxB,IAAA;AAAA,MACA,IAAA;AAAA,MACA,OAAA,EAAS;AAAA,QACP,WAAA,kBAAa,OAAA,6BAAS,aAAA;AAAA,QACtB,UAAA,kBAAY,OAAA,6BAAS,aAAA,EACjB,CAAC,KAAA,EAAA,GAAU;AACT,UAAA,OAAA,CAAQ,UAAA,CAAY;AAAA,YAClB,MAAA,mBAAS,KAAA,CAA8B,MAAA,UAAU,GAAA;AAAA,YACjD,KAAA,mBAAQ,KAAA,CAA6B,KAAA,UAAS;AAAA,UAChD,CAAC,CAAA;AAAA,QACH,EAAA,EACA,KAAA;AAAA,MACN;AAAA,IACF,CAAC,CAAA;AACD,IAAA,MAAM,MAAA,CAAO,MAAA;AACb,IAAA,OAAO,EAAE,KAAK,CAAA;AAAA,EAChB;AACF,CAAA;AH0FA;AACE;AACA;AACA;AACF,oKAAC","file":"/home/runner/work/orjok/orjok/packages/commons/dist/amplify.cjs","sourcesContent":[null,"import type { AuthProvider, UserAttributes, AuthSession } from \"../../providers/auth\";\n\nexport class AmplifyAuthProvider implements AuthProvider {\n async signIn(username: string, password: string) {\n const { signIn } = await import(\"aws-amplify/auth\");\n const result = await signIn({ username, password });\n return { isSignedIn: result.isSignedIn };\n }\n\n async signUp(username: string, password: string, attributes: Record<string, string>) {\n const { signUp } = await import(\"aws-amplify/auth\");\n const result = await signUp({\n username,\n password,\n options: { userAttributes: attributes },\n });\n return { isSignUpComplete: result.isSignUpComplete, userId: result.userId };\n }\n\n async signOut() {\n const { signOut } = await import(\"aws-amplify/auth\");\n await signOut();\n }\n\n async signInWithRedirect(provider: string) {\n const { signInWithRedirect } = await import(\"aws-amplify/auth\");\n await signInWithRedirect({ provider: { custom: provider } });\n }\n\n async fetchUserAttributes(): Promise<UserAttributes> {\n const { fetchUserAttributes } = await import(\"aws-amplify/auth\");\n const attrs = await fetchUserAttributes();\n return attrs as UserAttributes;\n }\n\n async updateUserAttributes(attributes: Record<string, string>) {\n const { updateUserAttributes } = await import(\"aws-amplify/auth\");\n const userAttributes: Record<string, string> = {};\n for (const [key, value] of Object.entries(attributes)) {\n userAttributes[key] = value;\n }\n await updateUserAttributes({ userAttributes });\n }\n\n async fetchAuthSession(): Promise<AuthSession> {\n const { fetchAuthSession } = await import(\"aws-amplify/auth\");\n const session = await fetchAuthSession();\n return session as AuthSession;\n }\n\n async confirmSignUp(username: string, code: string) {\n const { confirmSignUp } = await import(\"aws-amplify/auth\");\n const result = await confirmSignUp({ username, confirmationCode: code });\n return { isSignUpComplete: result.isSignUpComplete };\n }\n\n async resendSignUpCode(username: string) {\n const { resendSignUpCode } = await import(\"aws-amplify/auth\");\n await resendSignUpCode({ username });\n }\n\n async resetPassword(username: string) {\n const { resetPassword } = await import(\"aws-amplify/auth\");\n await resetPassword({ username });\n }\n\n async confirmResetPassword(username: string, code: string, newPassword: string) {\n const { confirmResetPassword } = await import(\"aws-amplify/auth\");\n await confirmResetPassword({ username, confirmationCode: code, newPassword });\n }\n\n async getCurrentUserId(): Promise<string | null> {\n try {\n const { getCurrentUser } = await import(\"aws-amplify/auth\");\n const user = await getCurrentUser();\n return user.userId;\n } catch {\n return null;\n }\n }\n}\n","import type { NetworkProvider, GraphQLResult } from \"../../providers/network\";\n\nexport class AmplifyNetworkProvider implements NetworkProvider {\n private async getAuthMode(): Promise<\"userPool\" | \"apiKey\"> {\n try {\n const { fetchAuthSession } = await import(\"aws-amplify/auth\");\n const session = await fetchAuthSession();\n if (session.tokens?.accessToken) return \"userPool\";\n } catch {\n // No session — fall through to apiKey\n }\n return \"apiKey\";\n }\n\n async query<T>(query: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>> {\n const { generateClient } = await import(\"aws-amplify/api\");\n const client = generateClient();\n const authMode = await this.getAuthMode();\n const result = await client.graphql({ query, variables: variables ?? {}, authMode });\n return result as GraphQLResult<T>;\n }\n\n async mutate<T>(mutation: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>> {\n const { generateClient } = await import(\"aws-amplify/api\");\n const client = generateClient();\n const authMode = await this.getAuthMode();\n const result = await client.graphql({ query: mutation, variables: variables ?? {}, authMode });\n return result as GraphQLResult<T>;\n }\n}\n","import type { StorageProvider, UploadOptions } from \"../../providers/storage\";\n\nexport class AmplifyStorageProvider implements StorageProvider {\n async getFileUrl(path: string): Promise<{ url: string }> {\n const { getUrl } = await import(\"aws-amplify/storage\");\n const result = await getUrl({ path });\n return { url: result.url.toString() };\n }\n\n async uploadFile(\n path: string,\n data: Blob | ArrayBuffer | string,\n options?: UploadOptions\n ): Promise<{ path: string }> {\n const { uploadData } = await import(\"aws-amplify/storage\");\n const result = uploadData({\n path,\n data,\n options: {\n contentType: options?.contentType,\n onProgress: options?.onProgress\n ? (event) => {\n options.onProgress!({\n loaded: (event as { loaded?: number }).loaded ?? 0,\n total: (event as { total?: number }).total ?? 0,\n });\n }\n : undefined,\n },\n });\n await result.result;\n return { path };\n }\n}\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { A as AuthProvider, U as UserAttributes, a as AuthSession, N as NetworkProvider, G as GraphQLResult, S as StorageProvider, b as UploadOptions } from './storage-DLqdJgQt.cjs';
|
|
2
|
+
|
|
3
|
+
declare class AmplifyAuthProvider implements AuthProvider {
|
|
4
|
+
signIn(username: string, password: string): Promise<{
|
|
5
|
+
isSignedIn: boolean;
|
|
6
|
+
}>;
|
|
7
|
+
signUp(username: string, password: string, attributes: Record<string, string>): Promise<{
|
|
8
|
+
isSignUpComplete: boolean;
|
|
9
|
+
userId: string | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
signOut(): Promise<void>;
|
|
12
|
+
signInWithRedirect(provider: string): Promise<void>;
|
|
13
|
+
fetchUserAttributes(): Promise<UserAttributes>;
|
|
14
|
+
updateUserAttributes(attributes: Record<string, string>): Promise<void>;
|
|
15
|
+
fetchAuthSession(): Promise<AuthSession>;
|
|
16
|
+
confirmSignUp(username: string, code: string): Promise<{
|
|
17
|
+
isSignUpComplete: boolean;
|
|
18
|
+
}>;
|
|
19
|
+
resendSignUpCode(username: string): Promise<void>;
|
|
20
|
+
resetPassword(username: string): Promise<void>;
|
|
21
|
+
confirmResetPassword(username: string, code: string, newPassword: string): Promise<void>;
|
|
22
|
+
getCurrentUserId(): Promise<string | null>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare class AmplifyNetworkProvider implements NetworkProvider {
|
|
26
|
+
private getAuthMode;
|
|
27
|
+
query<T>(query: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>>;
|
|
28
|
+
mutate<T>(mutation: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare class AmplifyStorageProvider implements StorageProvider {
|
|
32
|
+
getFileUrl(path: string): Promise<{
|
|
33
|
+
url: string;
|
|
34
|
+
}>;
|
|
35
|
+
uploadFile(path: string, data: Blob | ArrayBuffer | string, options?: UploadOptions): Promise<{
|
|
36
|
+
path: string;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { AmplifyAuthProvider, AmplifyNetworkProvider, AmplifyStorageProvider };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { A as AuthProvider, U as UserAttributes, a as AuthSession, N as NetworkProvider, G as GraphQLResult, S as StorageProvider, b as UploadOptions } from './storage-DLqdJgQt.js';
|
|
2
|
+
|
|
3
|
+
declare class AmplifyAuthProvider implements AuthProvider {
|
|
4
|
+
signIn(username: string, password: string): Promise<{
|
|
5
|
+
isSignedIn: boolean;
|
|
6
|
+
}>;
|
|
7
|
+
signUp(username: string, password: string, attributes: Record<string, string>): Promise<{
|
|
8
|
+
isSignUpComplete: boolean;
|
|
9
|
+
userId: string | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
signOut(): Promise<void>;
|
|
12
|
+
signInWithRedirect(provider: string): Promise<void>;
|
|
13
|
+
fetchUserAttributes(): Promise<UserAttributes>;
|
|
14
|
+
updateUserAttributes(attributes: Record<string, string>): Promise<void>;
|
|
15
|
+
fetchAuthSession(): Promise<AuthSession>;
|
|
16
|
+
confirmSignUp(username: string, code: string): Promise<{
|
|
17
|
+
isSignUpComplete: boolean;
|
|
18
|
+
}>;
|
|
19
|
+
resendSignUpCode(username: string): Promise<void>;
|
|
20
|
+
resetPassword(username: string): Promise<void>;
|
|
21
|
+
confirmResetPassword(username: string, code: string, newPassword: string): Promise<void>;
|
|
22
|
+
getCurrentUserId(): Promise<string | null>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare class AmplifyNetworkProvider implements NetworkProvider {
|
|
26
|
+
private getAuthMode;
|
|
27
|
+
query<T>(query: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>>;
|
|
28
|
+
mutate<T>(mutation: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare class AmplifyStorageProvider implements StorageProvider {
|
|
32
|
+
getFileUrl(path: string): Promise<{
|
|
33
|
+
url: string;
|
|
34
|
+
}>;
|
|
35
|
+
uploadFile(path: string, data: Blob | ArrayBuffer | string, options?: UploadOptions): Promise<{
|
|
36
|
+
path: string;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { AmplifyAuthProvider, AmplifyNetworkProvider, AmplifyStorageProvider };
|
package/dist/amplify.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/adapters/amplify/auth-provider.ts
|
|
2
|
+
var AmplifyAuthProvider = class {
|
|
3
|
+
async signIn(username, password) {
|
|
4
|
+
const { signIn } = await import("aws-amplify/auth");
|
|
5
|
+
const result = await signIn({ username, password });
|
|
6
|
+
return { isSignedIn: result.isSignedIn };
|
|
7
|
+
}
|
|
8
|
+
async signUp(username, password, attributes) {
|
|
9
|
+
const { signUp } = await import("aws-amplify/auth");
|
|
10
|
+
const result = await signUp({
|
|
11
|
+
username,
|
|
12
|
+
password,
|
|
13
|
+
options: { userAttributes: attributes }
|
|
14
|
+
});
|
|
15
|
+
return { isSignUpComplete: result.isSignUpComplete, userId: result.userId };
|
|
16
|
+
}
|
|
17
|
+
async signOut() {
|
|
18
|
+
const { signOut } = await import("aws-amplify/auth");
|
|
19
|
+
await signOut();
|
|
20
|
+
}
|
|
21
|
+
async signInWithRedirect(provider) {
|
|
22
|
+
const { signInWithRedirect } = await import("aws-amplify/auth");
|
|
23
|
+
await signInWithRedirect({ provider: { custom: provider } });
|
|
24
|
+
}
|
|
25
|
+
async fetchUserAttributes() {
|
|
26
|
+
const { fetchUserAttributes } = await import("aws-amplify/auth");
|
|
27
|
+
const attrs = await fetchUserAttributes();
|
|
28
|
+
return attrs;
|
|
29
|
+
}
|
|
30
|
+
async updateUserAttributes(attributes) {
|
|
31
|
+
const { updateUserAttributes } = await import("aws-amplify/auth");
|
|
32
|
+
const userAttributes = {};
|
|
33
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
34
|
+
userAttributes[key] = value;
|
|
35
|
+
}
|
|
36
|
+
await updateUserAttributes({ userAttributes });
|
|
37
|
+
}
|
|
38
|
+
async fetchAuthSession() {
|
|
39
|
+
const { fetchAuthSession } = await import("aws-amplify/auth");
|
|
40
|
+
const session = await fetchAuthSession();
|
|
41
|
+
return session;
|
|
42
|
+
}
|
|
43
|
+
async confirmSignUp(username, code) {
|
|
44
|
+
const { confirmSignUp } = await import("aws-amplify/auth");
|
|
45
|
+
const result = await confirmSignUp({ username, confirmationCode: code });
|
|
46
|
+
return { isSignUpComplete: result.isSignUpComplete };
|
|
47
|
+
}
|
|
48
|
+
async resendSignUpCode(username) {
|
|
49
|
+
const { resendSignUpCode } = await import("aws-amplify/auth");
|
|
50
|
+
await resendSignUpCode({ username });
|
|
51
|
+
}
|
|
52
|
+
async resetPassword(username) {
|
|
53
|
+
const { resetPassword } = await import("aws-amplify/auth");
|
|
54
|
+
await resetPassword({ username });
|
|
55
|
+
}
|
|
56
|
+
async confirmResetPassword(username, code, newPassword) {
|
|
57
|
+
const { confirmResetPassword } = await import("aws-amplify/auth");
|
|
58
|
+
await confirmResetPassword({ username, confirmationCode: code, newPassword });
|
|
59
|
+
}
|
|
60
|
+
async getCurrentUserId() {
|
|
61
|
+
try {
|
|
62
|
+
const { getCurrentUser } = await import("aws-amplify/auth");
|
|
63
|
+
const user = await getCurrentUser();
|
|
64
|
+
return user.userId;
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/adapters/amplify/network-provider.ts
|
|
72
|
+
var AmplifyNetworkProvider = class {
|
|
73
|
+
async getAuthMode() {
|
|
74
|
+
try {
|
|
75
|
+
const { fetchAuthSession } = await import("aws-amplify/auth");
|
|
76
|
+
const session = await fetchAuthSession();
|
|
77
|
+
if (session.tokens?.accessToken) return "userPool";
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
return "apiKey";
|
|
81
|
+
}
|
|
82
|
+
async query(query, variables) {
|
|
83
|
+
const { generateClient } = await import("aws-amplify/api");
|
|
84
|
+
const client = generateClient();
|
|
85
|
+
const authMode = await this.getAuthMode();
|
|
86
|
+
const result = await client.graphql({ query, variables: variables ?? {}, authMode });
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
async mutate(mutation, variables) {
|
|
90
|
+
const { generateClient } = await import("aws-amplify/api");
|
|
91
|
+
const client = generateClient();
|
|
92
|
+
const authMode = await this.getAuthMode();
|
|
93
|
+
const result = await client.graphql({ query: mutation, variables: variables ?? {}, authMode });
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/adapters/amplify/storage-provider.ts
|
|
99
|
+
var AmplifyStorageProvider = class {
|
|
100
|
+
async getFileUrl(path) {
|
|
101
|
+
const { getUrl } = await import("aws-amplify/storage");
|
|
102
|
+
const result = await getUrl({ path });
|
|
103
|
+
return { url: result.url.toString() };
|
|
104
|
+
}
|
|
105
|
+
async uploadFile(path, data, options) {
|
|
106
|
+
const { uploadData } = await import("aws-amplify/storage");
|
|
107
|
+
const result = uploadData({
|
|
108
|
+
path,
|
|
109
|
+
data,
|
|
110
|
+
options: {
|
|
111
|
+
contentType: options?.contentType,
|
|
112
|
+
onProgress: options?.onProgress ? (event) => {
|
|
113
|
+
options.onProgress({
|
|
114
|
+
loaded: event.loaded ?? 0,
|
|
115
|
+
total: event.total ?? 0
|
|
116
|
+
});
|
|
117
|
+
} : void 0
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
await result.result;
|
|
121
|
+
return { path };
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
export {
|
|
125
|
+
AmplifyAuthProvider,
|
|
126
|
+
AmplifyNetworkProvider,
|
|
127
|
+
AmplifyStorageProvider
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=amplify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapters/amplify/auth-provider.ts","../src/adapters/amplify/network-provider.ts","../src/adapters/amplify/storage-provider.ts"],"sourcesContent":["import type { AuthProvider, UserAttributes, AuthSession } from \"../../providers/auth\";\n\nexport class AmplifyAuthProvider implements AuthProvider {\n async signIn(username: string, password: string) {\n const { signIn } = await import(\"aws-amplify/auth\");\n const result = await signIn({ username, password });\n return { isSignedIn: result.isSignedIn };\n }\n\n async signUp(username: string, password: string, attributes: Record<string, string>) {\n const { signUp } = await import(\"aws-amplify/auth\");\n const result = await signUp({\n username,\n password,\n options: { userAttributes: attributes },\n });\n return { isSignUpComplete: result.isSignUpComplete, userId: result.userId };\n }\n\n async signOut() {\n const { signOut } = await import(\"aws-amplify/auth\");\n await signOut();\n }\n\n async signInWithRedirect(provider: string) {\n const { signInWithRedirect } = await import(\"aws-amplify/auth\");\n await signInWithRedirect({ provider: { custom: provider } });\n }\n\n async fetchUserAttributes(): Promise<UserAttributes> {\n const { fetchUserAttributes } = await import(\"aws-amplify/auth\");\n const attrs = await fetchUserAttributes();\n return attrs as UserAttributes;\n }\n\n async updateUserAttributes(attributes: Record<string, string>) {\n const { updateUserAttributes } = await import(\"aws-amplify/auth\");\n const userAttributes: Record<string, string> = {};\n for (const [key, value] of Object.entries(attributes)) {\n userAttributes[key] = value;\n }\n await updateUserAttributes({ userAttributes });\n }\n\n async fetchAuthSession(): Promise<AuthSession> {\n const { fetchAuthSession } = await import(\"aws-amplify/auth\");\n const session = await fetchAuthSession();\n return session as AuthSession;\n }\n\n async confirmSignUp(username: string, code: string) {\n const { confirmSignUp } = await import(\"aws-amplify/auth\");\n const result = await confirmSignUp({ username, confirmationCode: code });\n return { isSignUpComplete: result.isSignUpComplete };\n }\n\n async resendSignUpCode(username: string) {\n const { resendSignUpCode } = await import(\"aws-amplify/auth\");\n await resendSignUpCode({ username });\n }\n\n async resetPassword(username: string) {\n const { resetPassword } = await import(\"aws-amplify/auth\");\n await resetPassword({ username });\n }\n\n async confirmResetPassword(username: string, code: string, newPassword: string) {\n const { confirmResetPassword } = await import(\"aws-amplify/auth\");\n await confirmResetPassword({ username, confirmationCode: code, newPassword });\n }\n\n async getCurrentUserId(): Promise<string | null> {\n try {\n const { getCurrentUser } = await import(\"aws-amplify/auth\");\n const user = await getCurrentUser();\n return user.userId;\n } catch {\n return null;\n }\n }\n}\n","import type { NetworkProvider, GraphQLResult } from \"../../providers/network\";\n\nexport class AmplifyNetworkProvider implements NetworkProvider {\n private async getAuthMode(): Promise<\"userPool\" | \"apiKey\"> {\n try {\n const { fetchAuthSession } = await import(\"aws-amplify/auth\");\n const session = await fetchAuthSession();\n if (session.tokens?.accessToken) return \"userPool\";\n } catch {\n // No session — fall through to apiKey\n }\n return \"apiKey\";\n }\n\n async query<T>(query: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>> {\n const { generateClient } = await import(\"aws-amplify/api\");\n const client = generateClient();\n const authMode = await this.getAuthMode();\n const result = await client.graphql({ query, variables: variables ?? {}, authMode });\n return result as GraphQLResult<T>;\n }\n\n async mutate<T>(mutation: string, variables?: Record<string, unknown>): Promise<GraphQLResult<T>> {\n const { generateClient } = await import(\"aws-amplify/api\");\n const client = generateClient();\n const authMode = await this.getAuthMode();\n const result = await client.graphql({ query: mutation, variables: variables ?? {}, authMode });\n return result as GraphQLResult<T>;\n }\n}\n","import type { StorageProvider, UploadOptions } from \"../../providers/storage\";\n\nexport class AmplifyStorageProvider implements StorageProvider {\n async getFileUrl(path: string): Promise<{ url: string }> {\n const { getUrl } = await import(\"aws-amplify/storage\");\n const result = await getUrl({ path });\n return { url: result.url.toString() };\n }\n\n async uploadFile(\n path: string,\n data: Blob | ArrayBuffer | string,\n options?: UploadOptions\n ): Promise<{ path: string }> {\n const { uploadData } = await import(\"aws-amplify/storage\");\n const result = uploadData({\n path,\n data,\n options: {\n contentType: options?.contentType,\n onProgress: options?.onProgress\n ? (event) => {\n options.onProgress!({\n loaded: (event as { loaded?: number }).loaded ?? 0,\n total: (event as { total?: number }).total ?? 0,\n });\n }\n : undefined,\n },\n });\n await result.result;\n return { path };\n }\n}\n"],"mappings":";AAEO,IAAM,sBAAN,MAAkD;AAAA,EACvD,MAAM,OAAO,UAAkB,UAAkB;AAC/C,UAAM,EAAE,OAAO,IAAI,MAAM,OAAO,kBAAkB;AAClD,UAAM,SAAS,MAAM,OAAO,EAAE,UAAU,SAAS,CAAC;AAClD,WAAO,EAAE,YAAY,OAAO,WAAW;AAAA,EACzC;AAAA,EAEA,MAAM,OAAO,UAAkB,UAAkB,YAAoC;AACnF,UAAM,EAAE,OAAO,IAAI,MAAM,OAAO,kBAAkB;AAClD,UAAM,SAAS,MAAM,OAAO;AAAA,MAC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