Skip to content

MAKS11060/openapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAPI schemas

Unofficial OpenAPI schemas for some public APIs

Note

Danbooru

Typescript client with openapi-fetch

1. Generating types from OpenAPI schema

deno run -A npm:openapi-typescript \
  https://github.com/MAKS11060/openapi/releases/latest/download/danbooru.openapi.yaml \
  -o ./danbooru.oas.ts
npx openapi-typescript \
  https://github.com/MAKS11060/openapi/releases/latest/download/danbooru.openapi.yaml \
  -o ./danbooru.oas.ts

2. Create openapi-fetch client

// danbooru.ts
import createClient from 'openapi-fetch'
import type {paths} from './danbooru.oas.ts'

// Almost all GET requests do not require authorization.
// To use 'saved searches', you need an ApiKey.
// Register api key: https://danbooru.donmai.us/profile => API Key
// const login = ''
// const apiKey = ''
// const authorization = new TextEncoder().encode(`${login}:${apiKey}`).toBase64()

// deep serializer / {search: {id: [1,2]}} => ?search[id]=1,2
function querySerializer(
  obj: Record<string, unknown>,
  params: URLSearchParams = new URLSearchParams(),
  prefix = '',
): string {
  for (const [key, value] of Object.entries(obj)) {
    const encodedKey = encodeURIComponent(key)
    const paramKey = prefix ? `${prefix}[${encodedKey}]` : encodedKey
    if (value == null) continue
    if (Array.isArray(value)) {
      if (value.length === 0) continue
      params.append(paramKey, value.map(String).join(','))
    } else if (typeof value === 'object' && value !== null) {
      querySerializer(value as Record<string, unknown>, params, paramKey)
    } else {
      params.append(paramKey, String(value))
    }
  }

  return params.toString()
}

export const danbooruApi = createClient<paths>({
  baseUrl: 'https://danbooru.donmai.us',
  // headers: {authorization},
  querySerializer,
})

Shikimori

Typescript client with openapi-fetch

1. Generating types from OpenAPI schema

deno run -A npm:openapi-typescript \
  https://github.com/MAKS11060/openapi/releases/latest/download/shikimori.openapi.yaml \
  -o ./shikimori.oas.ts
npx openapi-typescript \
  https://github.com/MAKS11060/openapi/releases/latest/download/shikimori.openapi.yaml \
  -o ./shikimori.oas.ts
// shikimori.ts
import createClient from 'openapi-fetch'
import type {paths} from './shikimori.oas.ts'

// Requirements
// Add your Oauth2 Application name to User-Agent requests header.
// Don’t mimic a browser.
// Your IP address may be banned if you use API without properly set User-Agent header.
const shikimoriUserAgent = ''

export const shikimoriApi = createClient<paths>({
  baseUrl: 'https://shikimori.one',
  headers: {'user-agent': shikimoriUserAgent},
})

Build

Note

Project structure

  • src/{service}/
    • mod.ts - Entry point
    • openapi.ts - Config
    • schema.ts - Data models

Prerequisites

Install Deno

Install dependencies

deno run init

Check formatting and build

deno run ok

Build OpenAPI Schemas

deno run build

Build client types in example

deno run build:client