@osaas/client-mobile-backend 0.2.0
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/LICENSE +21 -0
- package/README.md +156 -0
- package/lib/__mocks__/@openauthjs/openauth/client.d.ts +8 -0
- package/lib/__mocks__/@openauthjs/openauth/client.d.ts.map +1 -0
- package/lib/__mocks__/@openauthjs/openauth/client.js +10 -0
- package/lib/__mocks__/@openauthjs/openauth/client.js.map +1 -0
- package/lib/auth.d.ts +26 -0
- package/lib/auth.d.ts.map +1 -0
- package/lib/auth.js +57 -0
- package/lib/auth.js.map +1 -0
- package/lib/database.d.ts +18 -0
- package/lib/database.d.ts.map +1 -0
- package/lib/database.js +81 -0
- package/lib/database.js.map +1 -0
- package/lib/image.d.ts +12 -0
- package/lib/image.d.ts.map +1 -0
- package/lib/image.js +26 -0
- package/lib/image.js.map +1 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +60 -0
- package/lib/index.js.map +1 -0
- package/lib/storage.d.ts +28 -0
- package/lib/storage.d.ts.map +1 -0
- package/lib/storage.js +59 -0
- package/lib/storage.js.map +1 -0
- package/lib/types.d.ts +24 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Eyevinn Technology Open Source Software Center
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# @osaas/client-mobile-backend
|
|
2
|
+
|
|
3
|
+
Client SDK for [Eyevinn Open Source Cloud](https://www.osaas.io) Mobile Backend solution. Provides unified access to auth, database, storage, and image transform services.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install @osaas/client-mobile-backend
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { MobileBackendClient } from '@osaas/client-mobile-backend';
|
|
15
|
+
|
|
16
|
+
const client = new MobileBackendClient({
|
|
17
|
+
sat: 'your-service-access-token',
|
|
18
|
+
auth: {
|
|
19
|
+
issuer: 'https://myauth.auto.prod.osaas.io',
|
|
20
|
+
clientID: 'my-app'
|
|
21
|
+
},
|
|
22
|
+
database: {
|
|
23
|
+
url: 'https://myrest.auto.prod.osaas.io'
|
|
24
|
+
},
|
|
25
|
+
storage: {
|
|
26
|
+
endpoint: 'https://myminio.auto.prod.osaas.io',
|
|
27
|
+
accessKey: 'minioadmin',
|
|
28
|
+
secretKey: 'your-secret',
|
|
29
|
+
bucket: 'app-assets'
|
|
30
|
+
},
|
|
31
|
+
imageTransform: {
|
|
32
|
+
url: 'https://myflyimg.auto.prod.osaas.io'
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
All config fields are optional — only configure the modules you use.
|
|
38
|
+
|
|
39
|
+
## Modules
|
|
40
|
+
|
|
41
|
+
### Auth
|
|
42
|
+
|
|
43
|
+
Wraps [@openauthjs/openauth](https://openauth.js.org/) for email + verification code authentication.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
// Start auth flow (redirects user to OpenAuth UI)
|
|
47
|
+
const { url, challenge } = await client.auth.authorize(
|
|
48
|
+
'https://myapp.com/callback',
|
|
49
|
+
{ pkce: true }
|
|
50
|
+
);
|
|
51
|
+
// Redirect user to `url`, store `challenge`
|
|
52
|
+
|
|
53
|
+
// After callback, exchange code for tokens
|
|
54
|
+
const result = await client.auth.exchange(
|
|
55
|
+
code,
|
|
56
|
+
redirectUri,
|
|
57
|
+
challenge?.verifier
|
|
58
|
+
);
|
|
59
|
+
if (!result.err) {
|
|
60
|
+
client.auth.setTokens(result.tokens);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Get current user ID (decoded from access token JWT)
|
|
64
|
+
const userId = client.auth.getUserId();
|
|
65
|
+
|
|
66
|
+
// Get Authorization header value
|
|
67
|
+
const authHeader = client.auth.getAuthHeader(); // "Bearer <token>"
|
|
68
|
+
|
|
69
|
+
// Refresh tokens
|
|
70
|
+
const refreshed = await client.auth.refresh(refreshToken);
|
|
71
|
+
|
|
72
|
+
// Verify a token against your subject schemas
|
|
73
|
+
const verified = await client.auth.verify(subjects, accessToken, {
|
|
74
|
+
refresh: refreshToken
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Database
|
|
79
|
+
|
|
80
|
+
Thin fetch wrapper for [PostgREST](https://postgrest.org/). Auto-attaches the SAT as an `Authorization: Bearer` header on every request.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// CRUD operations
|
|
84
|
+
const todos = await client.db.get<Todo[]>('/todos?user_id=eq.123');
|
|
85
|
+
const newTodo = await client.db.post<Todo>('/todos', {
|
|
86
|
+
title: 'Buy milk',
|
|
87
|
+
user_id: '123'
|
|
88
|
+
});
|
|
89
|
+
await client.db.patch('/todos?id=eq.5', { completed: true });
|
|
90
|
+
await client.db.delete('/todos?id=eq.5');
|
|
91
|
+
|
|
92
|
+
// Use with @supabase/postgrest-js for a query builder
|
|
93
|
+
import { PostgrestClient } from '@supabase/postgrest-js';
|
|
94
|
+
const postgrest = new PostgrestClient(client.db.baseUrl, {
|
|
95
|
+
fetch: client.db.fetch
|
|
96
|
+
});
|
|
97
|
+
const { data } = await postgrest.from('todos').select('*').eq('user_id', '123');
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Throws `DatabaseError` (with `.status`, `.statusText`, `.body`) on non-2xx responses.
|
|
101
|
+
|
|
102
|
+
### Storage
|
|
103
|
+
|
|
104
|
+
S3-compatible object storage via [MinIO](https://min.io/). Uses `@aws-sdk/client-s3` with `forcePathStyle: true`.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// Upload a file
|
|
108
|
+
const { key, url } = await client.storage.upload('avatars/photo.jpg', file, {
|
|
109
|
+
contentType: 'image/jpeg'
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Get a pre-signed download URL (default: 1 hour)
|
|
113
|
+
const downloadUrl = await client.storage.getSignedUrl('avatars/photo.jpg', {
|
|
114
|
+
expiresIn: 3600
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// List objects under a prefix
|
|
118
|
+
const objects = await client.storage.list('avatars/');
|
|
119
|
+
// objects: Array<{ key: string; size: number; lastModified: Date }>
|
|
120
|
+
|
|
121
|
+
// Delete an object
|
|
122
|
+
await client.storage.remove('avatars/photo.jpg');
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Image Transform
|
|
126
|
+
|
|
127
|
+
URL builder for [Flyimg](https://flyimg.io/) image transforms. Returns a URL string — no HTTP request is made.
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const thumbUrl = client.image.transform('https://example.com/photo.jpg', {
|
|
131
|
+
width: 200,
|
|
132
|
+
height: 200,
|
|
133
|
+
quality: 80,
|
|
134
|
+
crop: true
|
|
135
|
+
});
|
|
136
|
+
// => "https://myflyimg.auto.prod.osaas.io/upload/w_200,h_200,q_80,c_1/https://example.com/photo.jpg"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Service Access Token (SAT)
|
|
140
|
+
|
|
141
|
+
The `sat` field is passed as `Authorization: Bearer <SAT>` on database requests and can be used to gate access to SAT-protected services. The SAT controls service-level access; OpenAuth handles user identity separately.
|
|
142
|
+
|
|
143
|
+
## Zero Lock-In
|
|
144
|
+
|
|
145
|
+
This SDK wraps standard protocols:
|
|
146
|
+
|
|
147
|
+
- **Auth**: OpenID Connect via [OpenAuth](https://openauth.js.org/)
|
|
148
|
+
- **Database**: REST via [PostgREST](https://postgrest.org/)
|
|
149
|
+
- **Storage**: S3 API via [MinIO](https://min.io/)
|
|
150
|
+
- **Image**: HTTP URL construction via [Flyimg](https://flyimg.io/)
|
|
151
|
+
|
|
152
|
+
You can always use the underlying services directly without this SDK.
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
export declare const createClient: jest.Mock<{
|
|
3
|
+
authorize: jest.Mock<any, any, any>;
|
|
4
|
+
exchange: jest.Mock<any, any, any>;
|
|
5
|
+
verify: jest.Mock<any, any, any>;
|
|
6
|
+
refresh: jest.Mock<any, any, any>;
|
|
7
|
+
}, [], any>;
|
|
8
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../src/__mocks__/@openauthjs/openauth/client.ts"],"names":[],"mappings":";AAAA,eAAO,MAAM,YAAY;;;;;WAKtB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createClient = void 0;
|
|
4
|
+
exports.createClient = jest.fn(() => ({
|
|
5
|
+
authorize: jest.fn(),
|
|
6
|
+
exchange: jest.fn(),
|
|
7
|
+
verify: jest.fn(),
|
|
8
|
+
refresh: jest.fn()
|
|
9
|
+
}));
|
|
10
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../../src/__mocks__/@openauthjs/openauth/client.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;IACzC,SAAS,EAAE,IAAI,CAAC,EAAE,EAAE;IACpB,QAAQ,EAAE,IAAI,CAAC,EAAE,EAAE;IACnB,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE;IACjB,OAAO,EAAE,IAAI,CAAC,EAAE,EAAE;CACnB,CAAC,CAAC,CAAC"}
|
package/lib/auth.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AuthorizeResult, ExchangeSuccess, ExchangeError, RefreshSuccess, RefreshError, VerifyResult, VerifyError } from '@openauthjs/openauth/client';
|
|
2
|
+
export type { AuthorizeResult, ExchangeSuccess, ExchangeError, RefreshSuccess, RefreshError, VerifyResult, VerifyError };
|
|
3
|
+
export interface AuthTokens {
|
|
4
|
+
access: string;
|
|
5
|
+
refresh: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class AuthModule {
|
|
8
|
+
private openauth;
|
|
9
|
+
private tokens?;
|
|
10
|
+
constructor(issuer: string, clientID: string);
|
|
11
|
+
authorize(redirectUri: string, opts?: {
|
|
12
|
+
pkce?: boolean;
|
|
13
|
+
provider?: string;
|
|
14
|
+
}): Promise<AuthorizeResult>;
|
|
15
|
+
exchange(code: string, redirectUri: string, verifier?: string): Promise<ExchangeSuccess | ExchangeError>;
|
|
16
|
+
setTokens(tokens: AuthTokens): void;
|
|
17
|
+
verify(subjects: any, token: string, opts?: {
|
|
18
|
+
refresh?: string;
|
|
19
|
+
}): Promise<VerifyResult | VerifyError>;
|
|
20
|
+
refresh(refreshToken: string, opts?: {
|
|
21
|
+
access?: string;
|
|
22
|
+
}): Promise<RefreshSuccess | RefreshError>;
|
|
23
|
+
getUserId(): string | undefined;
|
|
24
|
+
getAuthHeader(): string | undefined;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,aAAa,EACb,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,WAAW,EACZ,MAAM,6BAA6B,CAAC;AAErC,YAAY,EACV,eAAe,EACf,eAAe,EACf,aAAa,EACb,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,WAAW,EACZ,CAAC;AAEF,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAkC;IAClD,OAAO,CAAC,MAAM,CAAC,CAAa;gBAEhB,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAItC,SAAS,CACb,WAAW,EAAE,MAAM,EACnB,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,GAC3C,OAAO,CAAC,eAAe,CAAC;IAIrB,QAAQ,CACZ,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,eAAe,GAAG,aAAa,CAAC;IAW3C,SAAS,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAK7B,MAAM,CAEV,QAAQ,EAAE,GAAG,EACb,KAAK,EAAE,MAAM,EACb,IAAI,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAC1B,OAAO,CAAC,YAAY,GAAG,WAAW,CAAC;IAIhC,OAAO,CACX,YAAY,EAAE,MAAM,EACpB,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,GACzB,OAAO,CAAC,cAAc,GAAG,YAAY,CAAC;IAIzC,SAAS,IAAI,MAAM,GAAG,SAAS;IAc/B,aAAa,IAAI,MAAM,GAAG,SAAS;CAIpC"}
|
package/lib/auth.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthModule = void 0;
|
|
4
|
+
const client_1 = require("@openauthjs/openauth/client");
|
|
5
|
+
class AuthModule {
|
|
6
|
+
openauth;
|
|
7
|
+
tokens;
|
|
8
|
+
constructor(issuer, clientID) {
|
|
9
|
+
this.openauth = (0, client_1.createClient)({ issuer, clientID });
|
|
10
|
+
}
|
|
11
|
+
async authorize(redirectUri, opts) {
|
|
12
|
+
return this.openauth.authorize(redirectUri, 'code', opts);
|
|
13
|
+
}
|
|
14
|
+
async exchange(code, redirectUri, verifier) {
|
|
15
|
+
const result = await this.openauth.exchange(code, redirectUri, verifier);
|
|
16
|
+
if (!result.err) {
|
|
17
|
+
this.tokens = {
|
|
18
|
+
access: result.tokens.access,
|
|
19
|
+
refresh: result.tokens.refresh
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
setTokens(tokens) {
|
|
25
|
+
this.tokens = tokens;
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
async verify(
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
subjects, token, opts) {
|
|
31
|
+
return this.openauth.verify(subjects, token, opts);
|
|
32
|
+
}
|
|
33
|
+
async refresh(refreshToken, opts) {
|
|
34
|
+
return this.openauth.refresh(refreshToken, opts);
|
|
35
|
+
}
|
|
36
|
+
getUserId() {
|
|
37
|
+
if (!this.tokens?.access)
|
|
38
|
+
return undefined;
|
|
39
|
+
try {
|
|
40
|
+
const parts = this.tokens.access.split('.');
|
|
41
|
+
if (parts.length !== 3)
|
|
42
|
+
return undefined;
|
|
43
|
+
const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString('utf-8'));
|
|
44
|
+
return payload.sub;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
getAuthHeader() {
|
|
51
|
+
if (!this.tokens?.access)
|
|
52
|
+
return undefined;
|
|
53
|
+
return `Bearer ${this.tokens.access}`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.AuthModule = AuthModule;
|
|
57
|
+
//# sourceMappingURL=auth.js.map
|
package/lib/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":";;;AAAA,wDAA2D;AA0B3D,MAAa,UAAU;IACb,QAAQ,CAAkC;IAC1C,MAAM,CAAc;IAE5B,YAAY,MAAc,EAAE,QAAgB;QAC1C,IAAI,CAAC,QAAQ,GAAG,IAAA,qBAAY,EAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,SAAS,CACb,WAAmB,EACnB,IAA4C;QAE5C,OAAO,IAAI,CAAC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CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAClD,CAAC;YACF,OAAO,OAAO,CAAC,GAAyB,CAAC;SAC1C;QAAC,MAAM;YACN,OAAO,SAAS,CAAC;SAClB;IACH,CAAC;IAED,aAAa;QACX,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM;YAAE,OAAO,SAAS,CAAC;QAC3C,OAAO,UAAU,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IACxC,CAAC;CACF;AArED,gCAqEC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class DatabaseError extends Error {
|
|
2
|
+
status: number;
|
|
3
|
+
statusText: string;
|
|
4
|
+
body: unknown;
|
|
5
|
+
constructor(status: number, statusText: string, body: unknown);
|
|
6
|
+
}
|
|
7
|
+
export declare class DatabaseModule {
|
|
8
|
+
readonly baseUrl: string;
|
|
9
|
+
private getSat;
|
|
10
|
+
constructor(baseUrl: string, getSat: () => string | undefined);
|
|
11
|
+
get fetch(): typeof globalThis.fetch;
|
|
12
|
+
private request;
|
|
13
|
+
get<T = unknown>(path: string): Promise<T>;
|
|
14
|
+
post<T = unknown>(path: string, body: unknown): Promise<T>;
|
|
15
|
+
patch<T = unknown>(path: string, body: unknown): Promise<T>;
|
|
16
|
+
delete(path: string): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=database.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAc,SAAQ,KAAK;IAE7B,MAAM,EAAE,MAAM;IACd,UAAU,EAAE,MAAM;IAClB,IAAI,EAAE,OAAO;gBAFb,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,OAAO;CAKvB;AAED,qBAAa,cAAc;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,MAAM,CAA2B;gBAE7B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,MAAM,GAAG,SAAS;IAK7D,IAAI,KAAK,IAAI,OAAO,UAAU,CAAC,KAAK,CAcnC;YAEa,OAAO;IAuCf,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IAI1C,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;IAI1D,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;IAI3D,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG1C"}
|
package/lib/database.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseModule = exports.DatabaseError = void 0;
|
|
4
|
+
class DatabaseError extends Error {
|
|
5
|
+
status;
|
|
6
|
+
statusText;
|
|
7
|
+
body;
|
|
8
|
+
constructor(status, statusText, body) {
|
|
9
|
+
super(`Database request failed: ${status} ${statusText}`);
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.statusText = statusText;
|
|
12
|
+
this.body = body;
|
|
13
|
+
this.name = 'DatabaseError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.DatabaseError = DatabaseError;
|
|
17
|
+
class DatabaseModule {
|
|
18
|
+
baseUrl;
|
|
19
|
+
getSat;
|
|
20
|
+
constructor(baseUrl, getSat) {
|
|
21
|
+
this.baseUrl = baseUrl.replace(/\/+$/, '');
|
|
22
|
+
this.getSat = getSat;
|
|
23
|
+
}
|
|
24
|
+
get fetch() {
|
|
25
|
+
return (input, init) => {
|
|
26
|
+
const headers = new Headers(init?.headers);
|
|
27
|
+
const sat = this.getSat();
|
|
28
|
+
if (sat) {
|
|
29
|
+
headers.set('Authorization', `Bearer ${sat}`);
|
|
30
|
+
}
|
|
31
|
+
headers.set('Content-Type', 'application/json');
|
|
32
|
+
headers.set('Accept', 'application/json');
|
|
33
|
+
return globalThis.fetch(input, { ...init, headers });
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async request(method, path, body) {
|
|
37
|
+
const url = `${this.baseUrl}${path}`;
|
|
38
|
+
const headers = {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
Accept: 'application/json'
|
|
41
|
+
};
|
|
42
|
+
const sat = this.getSat();
|
|
43
|
+
if (sat) {
|
|
44
|
+
headers['Authorization'] = `Bearer ${sat}`;
|
|
45
|
+
}
|
|
46
|
+
const response = await globalThis.fetch(url, {
|
|
47
|
+
method,
|
|
48
|
+
headers,
|
|
49
|
+
body: body !== undefined ? JSON.stringify(body) : undefined
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
const responseBody = await response.text().catch(() => '');
|
|
53
|
+
let parsed;
|
|
54
|
+
try {
|
|
55
|
+
parsed = JSON.parse(responseBody);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
parsed = responseBody;
|
|
59
|
+
}
|
|
60
|
+
throw new DatabaseError(response.status, response.statusText, parsed);
|
|
61
|
+
}
|
|
62
|
+
if (response.status === 204 || method === 'DELETE') {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return response.json();
|
|
66
|
+
}
|
|
67
|
+
async get(path) {
|
|
68
|
+
return this.request('GET', path);
|
|
69
|
+
}
|
|
70
|
+
async post(path, body) {
|
|
71
|
+
return this.request('POST', path, body);
|
|
72
|
+
}
|
|
73
|
+
async patch(path, body) {
|
|
74
|
+
return this.request('PATCH', path, body);
|
|
75
|
+
}
|
|
76
|
+
async delete(path) {
|
|
77
|
+
await this.request('DELETE', path);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.DatabaseModule = DatabaseModule;
|
|
81
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":";;;AAAA,MAAa,aAAc,SAAQ,KAAK;IAE7B;IACA;IACA;IAHT,YACS,MAAc,EACd,UAAkB,EAClB,IAAa;QAEpB,KAAK,CAAC,4BAA4B,MAAM,IAAI,UAAU,EAAE,CAAC,CAAC;QAJnD,WAAM,GAAN,MAAM,CAAQ;QACd,eAAU,GAAV,UAAU,CAAQ;QAClB,SAAI,GAAJ,IAAI,CAAS;QAGpB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AATD,sCASC;AAED,MAAa,cAAc;IAChB,OAAO,CAAS;IACjB,MAAM,CAA2B;IAEzC,YAAY,OAAe,EAAE,MAAgC;QAC3D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAC3C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,IAAI,KAAK;QACP,OAAO,CACL,KAA6C,EAC7C,IAAkB,EAClB,EAAE;YACF,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YAC3C,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;YAC1B,IAAI,GAAG,EAAE;gBACP,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,GAAG,EAAE,CAAC,CAAC;aAC/C;YACD,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAC;YAChD,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAC;YAC1C,OAAO,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,GAAG,IAAI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C,GAAG,CAAc,IAAY;QACjC,OAAO,IAAI,CAAC,OAAO,CAAI,KAAK,EAAE,IAAI,CAAC,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,IAAI,CAAc,IAAY,EAAE,IAAa;QACjD,OAAO,IAAI,CAAC,OAAO,CAAI,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,KAAK,CAAc,IAAY,EAAE,IAAa;QAClD,OAAO,IAAI,CAAC,OAAO,CAAI,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC9C,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAY;QACvB,MAAM,IAAI,CAAC,OAAO,CAAO,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC3C,CAAC;CACF;AA/ED,wCA+EC"}
|
package/lib/image.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface ImageTransformOptions {
|
|
2
|
+
width?: number;
|
|
3
|
+
height?: number;
|
|
4
|
+
quality?: number;
|
|
5
|
+
crop?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare class ImageModule {
|
|
8
|
+
private baseUrl;
|
|
9
|
+
constructor(baseUrl: string);
|
|
10
|
+
transform(sourceUrl: string, options?: ImageTransformOptions): string;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=image.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../src/image.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAAS;gBAEZ,OAAO,EAAE,MAAM;IAI3B,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,MAAM;CAY1E"}
|
package/lib/image.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImageModule = void 0;
|
|
4
|
+
class ImageModule {
|
|
5
|
+
baseUrl;
|
|
6
|
+
constructor(baseUrl) {
|
|
7
|
+
this.baseUrl = baseUrl.replace(/\/+$/, '');
|
|
8
|
+
}
|
|
9
|
+
transform(sourceUrl, options = {}) {
|
|
10
|
+
const params = [];
|
|
11
|
+
if (options.width !== undefined)
|
|
12
|
+
params.push(`w_${options.width}`);
|
|
13
|
+
if (options.height !== undefined)
|
|
14
|
+
params.push(`h_${options.height}`);
|
|
15
|
+
if (options.quality !== undefined)
|
|
16
|
+
params.push(`q_${options.quality}`);
|
|
17
|
+
if (options.crop !== undefined)
|
|
18
|
+
params.push(`c_${options.crop ? 1 : 0}`);
|
|
19
|
+
const paramString = params.length > 0 ? params.join(',') : '';
|
|
20
|
+
return paramString
|
|
21
|
+
? `${this.baseUrl}/upload/${paramString}/${sourceUrl}`
|
|
22
|
+
: `${this.baseUrl}/upload/${sourceUrl}`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.ImageModule = ImageModule;
|
|
26
|
+
//# sourceMappingURL=image.js.map
|
package/lib/image.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../src/image.ts"],"names":[],"mappings":";;;AAOA,MAAa,WAAW;IACd,OAAO,CAAS;IAExB,YAAY,OAAe;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,SAAS,CAAC,SAAiB,EAAE,UAAiC,EAAE;QAC9D,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS;YAAE,MAAM,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;QACnE,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS;YAAE,MAAM,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACrE,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS;YAAE,MAAM,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QACvE,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS;YAAE,MAAM,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAEzE,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9D,OAAO,WAAW;YAChB,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,WAAW,WAAW,IAAI,SAAS,EAAE;YACtD,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,WAAW,SAAS,EAAE,CAAC;IAC5C,CAAC;CACF;AAnBD,kCAmBC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MobileBackendConfig, AuthConfig, DatabaseConfig, StorageConfig, ImageTransformConfig } from './types';
|
|
2
|
+
import { ImageModule, ImageTransformOptions } from './image';
|
|
3
|
+
import { DatabaseModule, DatabaseError } from './database';
|
|
4
|
+
import { AuthModule, AuthTokens } from './auth';
|
|
5
|
+
import { StorageModule, UploadResult, StorageObject } from './storage';
|
|
6
|
+
export { MobileBackendConfig, AuthConfig, DatabaseConfig, StorageConfig, ImageTransformConfig, ImageModule, ImageTransformOptions, DatabaseModule, DatabaseError, AuthModule, AuthTokens, StorageModule, UploadResult, StorageObject };
|
|
7
|
+
export declare class MobileBackendClient {
|
|
8
|
+
private config;
|
|
9
|
+
private _image?;
|
|
10
|
+
private _db?;
|
|
11
|
+
private _auth?;
|
|
12
|
+
private _storage?;
|
|
13
|
+
constructor(config: MobileBackendConfig);
|
|
14
|
+
get image(): ImageModule;
|
|
15
|
+
get db(): DatabaseModule;
|
|
16
|
+
get auth(): AuthModule;
|
|
17
|
+
get storage(): StorageModule;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,cAAc,EACd,aAAa,EACb,oBAAoB,EACrB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,WAAW,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAEvE,OAAO,EACL,mBAAmB,EACnB,UAAU,EACV,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,WAAW,EACX,qBAAqB,EACrB,cAAc,EACd,aAAa,EACb,UAAU,EACV,UAAU,EACV,aAAa,EACb,YAAY,EACZ,aAAa,EACd,CAAC;AAEF,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,MAAM,CAAC,CAAc;IAC7B,OAAO,CAAC,GAAG,CAAC,CAAiB;IAC7B,OAAO,CAAC,KAAK,CAAC,CAAa;IAC3B,OAAO,CAAC,QAAQ,CAAC,CAAgB;gBAErB,MAAM,EAAE,mBAAmB;IAIvC,IAAI,KAAK,IAAI,WAAW,CAUvB;IAED,IAAI,EAAE,IAAI,cAAc,CAavB;IAED,IAAI,IAAI,IAAI,UAAU,CAarB;IAED,IAAI,OAAO,IAAI,aAAa,CAU3B;CACF"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MobileBackendClient = exports.StorageModule = exports.AuthModule = exports.DatabaseError = exports.DatabaseModule = exports.ImageModule = void 0;
|
|
4
|
+
const image_1 = require("./image");
|
|
5
|
+
Object.defineProperty(exports, "ImageModule", { enumerable: true, get: function () { return image_1.ImageModule; } });
|
|
6
|
+
const database_1 = require("./database");
|
|
7
|
+
Object.defineProperty(exports, "DatabaseModule", { enumerable: true, get: function () { return database_1.DatabaseModule; } });
|
|
8
|
+
Object.defineProperty(exports, "DatabaseError", { enumerable: true, get: function () { return database_1.DatabaseError; } });
|
|
9
|
+
const auth_1 = require("./auth");
|
|
10
|
+
Object.defineProperty(exports, "AuthModule", { enumerable: true, get: function () { return auth_1.AuthModule; } });
|
|
11
|
+
const storage_1 = require("./storage");
|
|
12
|
+
Object.defineProperty(exports, "StorageModule", { enumerable: true, get: function () { return storage_1.StorageModule; } });
|
|
13
|
+
class MobileBackendClient {
|
|
14
|
+
config;
|
|
15
|
+
_image;
|
|
16
|
+
_db;
|
|
17
|
+
_auth;
|
|
18
|
+
_storage;
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.config = config;
|
|
21
|
+
}
|
|
22
|
+
get image() {
|
|
23
|
+
if (!this._image) {
|
|
24
|
+
if (!this.config.imageTransform) {
|
|
25
|
+
throw new Error('imageTransform configuration is required to use the image module');
|
|
26
|
+
}
|
|
27
|
+
this._image = new image_1.ImageModule(this.config.imageTransform.url);
|
|
28
|
+
}
|
|
29
|
+
return this._image;
|
|
30
|
+
}
|
|
31
|
+
get db() {
|
|
32
|
+
if (!this._db) {
|
|
33
|
+
if (!this.config.database) {
|
|
34
|
+
throw new Error('database configuration is required to use the database module');
|
|
35
|
+
}
|
|
36
|
+
this._db = new database_1.DatabaseModule(this.config.database.url, () => this.config.sat);
|
|
37
|
+
}
|
|
38
|
+
return this._db;
|
|
39
|
+
}
|
|
40
|
+
get auth() {
|
|
41
|
+
if (!this._auth) {
|
|
42
|
+
if (!this.config.auth) {
|
|
43
|
+
throw new Error('auth configuration is required to use the auth module');
|
|
44
|
+
}
|
|
45
|
+
this._auth = new auth_1.AuthModule(this.config.auth.issuer, this.config.auth.clientID);
|
|
46
|
+
}
|
|
47
|
+
return this._auth;
|
|
48
|
+
}
|
|
49
|
+
get storage() {
|
|
50
|
+
if (!this._storage) {
|
|
51
|
+
if (!this.config.storage) {
|
|
52
|
+
throw new Error('storage configuration is required to use the storage module');
|
|
53
|
+
}
|
|
54
|
+
this._storage = new storage_1.StorageModule(this.config.storage);
|
|
55
|
+
}
|
|
56
|
+
return this._storage;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.MobileBackendClient = MobileBackendClient;
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAOA,mCAA6D;AAW3D,4FAXO,mBAAW,OAWP;AAVb,yCAA2D;AAYzD,+FAZO,yBAAc,OAYP;AACd,8FAbuB,wBAAa,OAavB;AAZf,iCAAgD;AAa9C,2FAbO,iBAAU,OAaP;AAZZ,uCAAuE;AAcrE,8FAdO,uBAAa,OAcP;AAKf,MAAa,mBAAmB;IACtB,MAAM,CAAsB;IAC5B,MAAM,CAAe;IACrB,GAAG,CAAkB;IACrB,KAAK,CAAc;IACnB,QAAQ,CAAiB;IAEjC,YAAY,MAA2B;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,IAAI,KAAK;QACP,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAChB,IAAI,CAAC,IA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,CAAC,OAAO,CAAC,CAAC;SACxD;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF;AAhED,kDAgEC"}
|
package/lib/storage.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface UploadResult {
|
|
2
|
+
key: string;
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
export interface StorageObject {
|
|
6
|
+
key: string;
|
|
7
|
+
size: number;
|
|
8
|
+
lastModified: Date;
|
|
9
|
+
}
|
|
10
|
+
export declare class StorageModule {
|
|
11
|
+
private client;
|
|
12
|
+
private bucket;
|
|
13
|
+
constructor(config: {
|
|
14
|
+
endpoint: string;
|
|
15
|
+
accessKey: string;
|
|
16
|
+
secretKey: string;
|
|
17
|
+
bucket: string;
|
|
18
|
+
});
|
|
19
|
+
upload(key: string, body: Blob | Uint8Array | ReadableStream, opts?: {
|
|
20
|
+
contentType?: string;
|
|
21
|
+
}): Promise<UploadResult>;
|
|
22
|
+
getSignedUrl(key: string, opts?: {
|
|
23
|
+
expiresIn?: number;
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
list(prefix?: string): Promise<StorageObject[]>;
|
|
26
|
+
remove(key: string): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,IAAI,CAAC;CACpB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,EAAE;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;KAChB;IAaK,MAAM,CACV,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,IAAI,GAAG,UAAU,GAAG,cAAc,EACxC,IAAI,CAAC,EAAE;QAAE,WAAW,CAAC,EAAE,MAAM,CAAA;KAAE,GAC9B,OAAO,CAAC,YAAY,CAAC;IAelB,YAAY,CAChB,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAC5B,OAAO,CAAC,MAAM,CAAC;IAWZ,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAmB/C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAQzC"}
|
package/lib/storage.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StorageModule = void 0;
|
|
4
|
+
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
5
|
+
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
6
|
+
class StorageModule {
|
|
7
|
+
client;
|
|
8
|
+
bucket;
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.bucket = config.bucket;
|
|
11
|
+
this.client = new client_s3_1.S3Client({
|
|
12
|
+
endpoint: config.endpoint,
|
|
13
|
+
region: 'us-east-1',
|
|
14
|
+
credentials: {
|
|
15
|
+
accessKeyId: config.accessKey,
|
|
16
|
+
secretAccessKey: config.secretKey
|
|
17
|
+
},
|
|
18
|
+
forcePathStyle: true
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async upload(key, body, opts) {
|
|
22
|
+
await this.client.send(new client_s3_1.PutObjectCommand({
|
|
23
|
+
Bucket: this.bucket,
|
|
24
|
+
Key: key,
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
Body: body,
|
|
27
|
+
ContentType: opts?.contentType
|
|
28
|
+
}));
|
|
29
|
+
const url = await this.getSignedUrl(key);
|
|
30
|
+
return { key, url };
|
|
31
|
+
}
|
|
32
|
+
async getSignedUrl(key, opts) {
|
|
33
|
+
return (0, s3_request_presigner_1.getSignedUrl)(this.client, new client_s3_1.GetObjectCommand({
|
|
34
|
+
Bucket: this.bucket,
|
|
35
|
+
Key: key
|
|
36
|
+
}), { expiresIn: opts?.expiresIn ?? 3600 });
|
|
37
|
+
}
|
|
38
|
+
async list(prefix) {
|
|
39
|
+
const response = await this.client.send(new client_s3_1.ListObjectsV2Command({
|
|
40
|
+
Bucket: this.bucket,
|
|
41
|
+
Prefix: prefix
|
|
42
|
+
}));
|
|
43
|
+
return (response.Contents ?? [])
|
|
44
|
+
.filter((obj) => obj.Key !== undefined)
|
|
45
|
+
.map((obj) => ({
|
|
46
|
+
key: obj.Key,
|
|
47
|
+
size: obj.Size ?? 0,
|
|
48
|
+
lastModified: obj.LastModified ?? new Date(0)
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
async remove(key) {
|
|
52
|
+
await this.client.send(new client_s3_1.DeleteObjectCommand({
|
|
53
|
+
Bucket: this.bucket,
|
|
54
|
+
Key: key
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.StorageModule = StorageModule;
|
|
59
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":";;;AAAA,kDAM4B;AAC5B,wEAA6D;AAa7D,MAAa,aAAa;IAChB,MAAM,CAAW;IACjB,MAAM,CAAS;IAEvB,YAAY,MAKX;QACC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,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,CAAC,GAAG,EAAuC,EAAE,CAAC,GAAG,CAAC,GAAG,KAAK,SAAS,CACpE;aACA,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACb,GAAG,EAAE,GAAG,CAAC,GAAG;YACZ,IAAI,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC;YACnB,YAAY,EAAE,GAAG,CAAC,YAAY,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC;SAC9C,CAAC,CAAC,CAAC;IACR,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAW;QACtB,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CACpB,IAAI,+BAAmB,CAAC;YACtB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,GAAG,EAAE,GAAG;SACT,CAAC,CACH,CAAC;IACJ,CAAC;CACF;AAlFD,sCAkFC"}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface AuthConfig {
|
|
2
|
+
issuer: string;
|
|
3
|
+
clientID: string;
|
|
4
|
+
}
|
|
5
|
+
export interface DatabaseConfig {
|
|
6
|
+
url: string;
|
|
7
|
+
}
|
|
8
|
+
export interface StorageConfig {
|
|
9
|
+
endpoint: string;
|
|
10
|
+
accessKey: string;
|
|
11
|
+
secretKey: string;
|
|
12
|
+
bucket: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ImageTransformConfig {
|
|
15
|
+
url: string;
|
|
16
|
+
}
|
|
17
|
+
export interface MobileBackendConfig {
|
|
18
|
+
sat?: string;
|
|
19
|
+
auth?: AuthConfig;
|
|
20
|
+
database?: DatabaseConfig;
|
|
21
|
+
storage?: StorageConfig;
|
|
22
|
+
imageTransform?: ImageTransformConfig;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,mBAAmB;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACvC"}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@osaas/client-mobile-backend",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "OSC Mobile Backend client SDK - auth, database, storage, and image transforms",
|
|
5
|
+
"author": "Eyevinn Open Source Cloud <osc@eyevinn.se>",
|
|
6
|
+
"homepage": "https://www.osaas.io",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"types": "lib/index.d.ts",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/EyevinnOSC/client-ts.git"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"lint": "eslint .",
|
|
20
|
+
"pretty": "prettier --check --ignore-unknown .",
|
|
21
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
22
|
+
"test": "jest --passWithNoTests"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@aws-sdk/client-s3": "^3.1010.0",
|
|
29
|
+
"@aws-sdk/s3-request-presigner": "^3.1010.0",
|
|
30
|
+
"@openauthjs/openauth": "^0.4.3"
|
|
31
|
+
},
|
|
32
|
+
"gitHead": "0ab121dc68e7ff6b725049bc1dcb37dbed915681"
|
|
33
|
+
}
|