Code Monkey home page Code Monkey logo

ts-api-docs's Introduction

TypeScript API Docs

이 저장소는 TypeScript에서 타입 안전한 API fetcher를 생성하고 사용하는 방법에 대한 예제를 제공합니다. 자세한 설명과 블로그 게시물을 보려면 블로그를 방문하세요.

개요

이 프로젝트는 JSONPlaceholderGitHub API와 같은 다양한 서비스에 대한 API fetcher를 TypeScript로 생성하는 예제를 포함합니다. 이 fetcher들은 타입 안전성을 보장하고, API와 상호 작용하는 깔끔하고 유지보수 가능한 방법을 제공합니다.

type GitHubApiSpec =
  | ApiEndpoint<'GET', '/repos/:owner/:repo', { repo: string }, RepoDto>
  | ApiEndpoint<'GET', '/users/:username', { username: string }, UserDto>
  | ApiEndpoint<'POST', '/repos/:owner/:repo/issues', { owner: string }, IssueDto[]>;

export const GitHubApi = createFetcher<GitHubApiSpec>('https://api.github.com');

type JSONPlaceholderApiSpec =
  | ApiEndpoint<'GET', '/todos', void, TodoDto[]>
  | ApiEndpoint<'GET', '/todos/:id', void, TodoDto>
  | ApiEndpoint<'POST', '/todos', { title: string; userId: number; completed: boolean }, TodoDto>
  | ApiEndpoint<'GET', '/comments', void, CommentDto[]>
  | ApiEndpoint<'GET', '/comments/:id', void, CommentDto>
  | ApiEndpoint<'POST', '/comments', { name: string; email: string; body: string; postId: number }, CommentDto>;

export const JsonPlaceHolderApi = createFetcher<JSONPlaceholderApiSpec>('https://jsonplaceholder.typicode.com');

example infer-url infer-response

테스트

API fetcher에 대한 테스트가 포함되어 있습니다. 테스트를 실행하려면 다음 명령어를 사용합니다

pnpm install
pnpm test

# ✓ test/github-api.test.ts (2)
# ✓ test/json-place-holder-api.test.ts (6) 1267ms
#   Test Files  2 passed (2)
#        Tests  8 passed (8)

프로젝트 구조

.
├── src
│   ├── api-docs.d.ts
│   └── example
│       ├── github-example.api.ts
│       ├── helper
│       │   └── create-fetcher.ts
│       └── json-placeholder-example.api.ts
├── test
│   ├── github-api.test.ts
│   └── json-place-holder-api.test.ts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.