-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
111 lines (99 loc) · 2.81 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { expandGlob } from "std/fs/mod.ts";
import { calculateTag, loadLexicon } from "./lib/utils.ts";
import {
convertArray,
convertObject,
convertProcedure,
convertQuery,
convertRecord,
convertString,
convertToken,
} from "./lib/converters/mod.ts";
import type { OpenAPIV3_1 } from "openapi-types";
const entries = expandGlob("./lexicons/**/*.json");
const paths: OpenAPIV3_1.PathsObject = {};
const components: OpenAPIV3_1.ComponentsObject = {
schemas: {},
securitySchemes: {
Bearer: {
type: "http",
scheme: "bearer",
},
},
};
const tagNames = new Set<string>();
for await (const entry of entries) {
const doc = await loadLexicon(entry);
const id = doc.id;
const defs = doc.defs;
console.info(id);
tagNames.add(calculateTag(id));
for (const [name, def] of Object.entries(defs)) {
const identifier = name === "main" ? id : `${id}.${name}`;
switch (def.type) {
case "array":
components.schemas![identifier] = convertArray(id, name, def);
break;
case "object":
components.schemas![identifier] = convertObject(id, name, def);
break;
case "procedure": {
const post = await convertProcedure(id, name, def);
if (post) {
// @ts-ignore FIXME: Also confused about ArraySchemaObject
paths[`/${id}`] = { post };
}
break;
}
case "query": {
const get = await convertQuery(id, name, def);
if (get) {
// @ts-ignore FIXME: Also confused about ArraySchemaObject
paths[`/${id}`] = { get };
}
break;
}
case "record":
components.schemas![identifier] = convertRecord(id, name, def);
break;
case "string":
components.schemas![identifier] = convertString(id, name, def);
break;
case "subscription":
// No way to represent this in OpenAPI
break;
case "token":
components.schemas![identifier] = convertToken(id, name, def);
break;
default:
throw new Error(`Unknown type: ${def.type}`);
}
}
}
const api: OpenAPIV3_1.Document = {
openapi: "3.1.0",
info: {
title: "AT Protocol XRPC API",
summary:
"An unofficial conversion of AT Protocol's lexicons to OpenAPI's schema format.",
version: "0.0.0", // This will be a living document for now, so no versioning yet
license: {
name: "MIT License",
identifier: "MIT",
},
},
servers: [
{
url: "https://bsky.social/xrpc/",
description: "AT Protocol PDS XRPC server",
},
{
url: "https://api.bsky.app/xrpc/",
description: "AT Protocol AppView XRPC server",
},
],
paths,
components,
tags: Array.from(tagNames).map((name) => ({ name })),
};
Deno.writeTextFile("./spec/api.json", JSON.stringify(api, null, 2) + "\n");