@mootup/moot-sdk 0.1.0-rc.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/README.md +82 -0
- package/dist/client.d.ts +11 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +22 -0
- package/dist/client.js.map +1 -0
- package/dist/cookies.d.ts +10 -0
- package/dist/cookies.d.ts.map +1 -0
- package/dist/cookies.js +35 -0
- package/dist/cookies.js.map +1 -0
- package/dist/generated/paths.d.ts +4156 -0
- package/dist/generated/paths.d.ts.map +1 -0
- package/dist/generated/paths.js +6 -0
- package/dist/generated/paths.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/openapi.yaml +3059 -0
- package/package.json +48 -0
- package/src/client.ts +37 -0
- package/src/cookies.ts +37 -0
- package/src/generated/paths.ts +4156 -0
- package/src/index.ts +3 -0
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mootup/moot-sdk",
|
|
3
|
+
"version": "0.1.0-rc.0",
|
|
4
|
+
"description": "Typed HTTP client for the convo (mootup) API, generated from OpenAPI 3.1.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"openapi.yaml",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20.0.0"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"generate": "openapi-typescript openapi.yaml -o src/generated/paths.ts",
|
|
26
|
+
"sync:oas": "node scripts/sync-oas.mjs",
|
|
27
|
+
"build": "npm run generate && tsc -p tsconfig.build.json",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"lint": "tsc --noEmit",
|
|
31
|
+
"prepublishOnly": "npm run build && npm test"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"openapi-fetch": "^0.13.0",
|
|
35
|
+
"tough-cookie": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^20.14.0",
|
|
39
|
+
"@types/tough-cookie": "^4.0.5",
|
|
40
|
+
"msw": "^2.4.0",
|
|
41
|
+
"openapi-typescript": "^7.4.0",
|
|
42
|
+
"typescript": "^5.5.0",
|
|
43
|
+
"vitest": "^2.1.0"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import createClient from 'openapi-fetch';
|
|
2
|
+
import type { paths } from './generated/paths.js';
|
|
3
|
+
import { makeCookieJarMiddleware } from './cookies.js';
|
|
4
|
+
|
|
5
|
+
export interface MootupClientOptions {
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
fetch?: typeof globalThis.fetch;
|
|
9
|
+
persistCookies?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type MootupClient = ReturnType<typeof createMootupClient>;
|
|
13
|
+
|
|
14
|
+
export function createMootupClient(opts: MootupClientOptions) {
|
|
15
|
+
const client = createClient<paths>({
|
|
16
|
+
baseUrl: opts.baseUrl,
|
|
17
|
+
fetch: opts.fetch,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
if (opts.apiKey) {
|
|
21
|
+
const token = opts.apiKey;
|
|
22
|
+
client.use({
|
|
23
|
+
async onRequest({ request }) {
|
|
24
|
+
request.headers.set('Authorization', `Bearer ${token}`);
|
|
25
|
+
return request;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (opts.persistCookies) {
|
|
31
|
+
client.use(makeCookieJarMiddleware());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return client;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type { paths, components, operations } from './generated/paths.js';
|
package/src/cookies.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CookieJar } from 'tough-cookie';
|
|
2
|
+
import type { Middleware } from 'openapi-fetch';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build an openapi-fetch middleware that persists Set-Cookie headers across
|
|
6
|
+
* requests using an in-memory tough-cookie jar. Used by the /auth/request →
|
|
7
|
+
* /api/actors/me session flow in Node environments (browsers handle cookies
|
|
8
|
+
* natively).
|
|
9
|
+
*/
|
|
10
|
+
export function makeCookieJarMiddleware(jar = new CookieJar()): Middleware {
|
|
11
|
+
return {
|
|
12
|
+
async onRequest({ request }) {
|
|
13
|
+
const cookieHeader = await jar.getCookieString(request.url);
|
|
14
|
+
if (cookieHeader) {
|
|
15
|
+
request.headers.set('Cookie', cookieHeader);
|
|
16
|
+
}
|
|
17
|
+
return request;
|
|
18
|
+
},
|
|
19
|
+
async onResponse({ response, request }) {
|
|
20
|
+
const setCookie = response.headers.get('set-cookie');
|
|
21
|
+
if (setCookie) {
|
|
22
|
+
for (const cookie of splitSetCookieHeader(setCookie)) {
|
|
23
|
+
await jar.setCookie(cookie, request.url);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return response;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function splitSetCookieHeader(value: string): string[] {
|
|
32
|
+
// Set-Cookie values can include commas inside expires/date attributes,
|
|
33
|
+
// which is why most stdlibs expose the raw header array separately. Node's
|
|
34
|
+
// fetch flattens to a single string. Split on commas that are followed by
|
|
35
|
+
// a cookie-attribute-looking prefix (word chars + '=').
|
|
36
|
+
return value.split(/,(?=\s*[A-Za-z0-9_-]+=)/);
|
|
37
|
+
}
|