Files
supabase/apps/reference/generator/api.ts
Copple e486815ab0 Docs: hide generated (#9078)
* removes all generated files

* ignore all generated files

* moves the docs generator into docusaurus

* clean up old files from the generator

* generator -> parser

* Adds the latest spec

* fixing generated path

* updates specs

* delete generated files

* keep the generated folder

* keep generated folder

* Adds a dart generator

* Addds new auth doc for Dart v1

* Adds postgrest method in Dart doc

* Adds realtime docs to Dart v1

* Adds storage docs to Dart doc

* Adds filters and modifiers to Dart docs spec file

* fix: label used in dart sidebar to match spec file for Dart docs

* final items for generator

* Adds a build command

* Adds typescript

* fix broken links

* fix build

* fix import error

* lowercase helpers

* clean missing file

Co-authored-by: dshukertjr <18113850+dshukertjr@users.noreply.github.com>
2022-09-23 11:40:07 +02:00

139 lines
3.3 KiB
TypeScript

import template from './templates/ApiTemplate'
import { slugify, toArrayWithKey, toTitle, writeToDisk } from './helpers'
import { OpenAPIV3, OpenAPIV2 } from 'openapi-types'
import * as fs from 'fs'
import * as ejs from 'ejs'
export default async function gen(
inputFileName: string,
outputDir: string,
apiUrl: string
) {
const specRaw = fs.readFileSync(inputFileName, 'utf8')
const spec = JSON.parse(specRaw)
// console.log('spec', spec)
switch (spec.openapi || spec.swagger) {
case '3.0.0':
case '3.0.3':
await gen_v3(spec, outputDir, { apiUrl })
break
case '2.0':
await gen_v2(spec, outputDir, { apiUrl })
break
default:
console.log('Unrecognized specification version:', spec.openapi)
break
}
}
/**
* Versioned Generator
*/
// OPENAPI-SPEC-VERSION: 3.0.0
type v3OperationWithPath = OpenAPIV3.OperationObject & {
path: string
}
type enrichedOperation = OpenAPIV3.OperationObject & {
path: string
fullPath: string
operationId: string
}
async function gen_v3(
spec: OpenAPIV3.Document,
dest: string,
{ apiUrl }: { apiUrl: string }
) {
const specLayout = spec.tags || []
const operations: enrichedOperation[] = []
Object.entries(spec.paths).forEach(([key, val]) => {
const fullPath = `${apiUrl}${key}`
toArrayWithKey(val!, 'operation').forEach((o) => {
const operation = o as v3OperationWithPath
const enriched = {
...operation,
path: key,
fullPath,
operationId: slugify(operation.summary!),
responseList:
toArrayWithKey(operation.responses!, 'responseCode') || [],
}
operations.push(enriched)
})
})
const sections = specLayout.map((section) => {
return {
...section,
title: toTitle(section.name),
id: slugify(section.name),
operations: operations.filter((operation) =>
operation.tags?.includes(section.name)
),
}
})
const content = ejs.render(template, {
info: spec.info,
sections,
operations,
})
// console.log(content)
// Write to disk
await writeToDisk(dest, content)
console.log('Saved: ', dest)
}
// OPENAPI-SPEC-VERSION: 2.0
async function gen_v2(
spec: OpenAPIV2.Document,
dest: string,
{ apiUrl }: { apiUrl: string }
) {
const specLayout = spec.tags || []
const operations: enrichedOperation[] = []
Object.entries(spec.paths).forEach(([key, val]) => {
const fullPath = `${apiUrl}${key}`
toArrayWithKey(val!, 'operation').forEach((o) => {
const operation = o as v3OperationWithPath
const enriched = {
...operation,
path: key,
fullPath,
operationId: slugify(operation.summary!),
responseList:
toArrayWithKey(operation.responses!, 'responseCode') || [],
}
operations.push(enriched)
})
})
const sections = specLayout.map((section) => {
return {
...section,
title: toTitle(section.name),
id: slugify(section.name),
operations: operations.filter((operation) =>
operation.tags?.includes(section.name)
),
}
})
const content = ejs.render(template, {
info: spec.info,
sections,
operations,
})
// console.log(content)
// Write to disk
await writeToDisk(dest, content)
console.log('Saved: ', dest)
}