Skip to content

MAKS11060/openapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 {components, paths} from './danbooru.oas.ts'

export type Danbooru = components['schemas']

// 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()

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

// 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()
}

Moebooru

A group of services based on moebooru

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/moebooru.openapi.yaml \
  -o ./moebooru.oas.ts
npx openapi-typescript \
  https://github.com/MAKS11060/openapi/releases/latest/download/moebooru.openapi.yaml \
  -o ./moebooru.oas.ts

2. Create openapi-fetch client

// moebooru.ts
import createClient from 'openapi-fetch'
import type {components, paths} from './moebooru.oas.ts'

export type Moebooru = components['schemas']

export const moebooruApi = createClient<paths>({
  baseUrl: 'https://yande.re',
  // baseUrl: 'https://konachan.com',
  // baseUrl: 'https://konachan.net',
})

// handle '/post.json?api_version=2'
api.use({ // merge two query parameters '?ver=2?tags=id:1' => '?ver=2&tags=id:1'
  onRequest({request}) {
    const url = new URL(request.url)
    if (url.search.slice(1).includes('?')) {
      url.search = url.search.slice(1).replace('?', '&')
    }
    return new Request(url, request)
  },
})

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 {components, paths} from './shikimori.oas.ts'

export type Shikimori = components['schemas']

// 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