npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ahhaohho/response-dto

v1.1.1

Published

AhhaOhho API 응답을 위한 DTO 클래스

Downloads

239

Readme

ResponseDTO

AhhaOhho API 응답을 위한 DTO(Data Transfer Object) 클래스입니다.

소개

이 패키지는 AhhaOhho API 응답을 표준화하고 일관성 있게 만들기 위한 ResponseDTO 클래스를 제공합니다. 성공 응답, 클라이언트 오류, 시스템 오류 등 다양한 API 응답 상황을 처리할 수 있습니다. 모든 응답은 일관된 구조를 가지며, 데이터가 없는 경우에도 null을 명시적으로 포함하여 타입 안전성을 보장합니다.

설치

npm을 사용하여 패키지를 설치할 수 있습니다:

npm install @ahhaohho/response-dto

사용 방법

ResponseDTO 클래스를 import하여 사용할 수 있습니다:

const ResponseDTO = require('@ahhaohho/response-dto');

// 성공 응답 예시 (데이터 있음)
const successResponse = ResponseDTO.success({ user: { id: 1, name: '아하오호' } });

// 성공 응답 예시 (데이터 없음)
const noDataResponse = ResponseDTO.success();

// 클라이언트 오류 응답 예시
const clientErrorResponse = ResponseDTO.badRequest('잘못된 요청입니다.');

// 시스템 오류 응답 예시
const systemErrorResponse = ResponseDTO.systemError('서버 오류가 발생했습니다.');

주요 기능

  • success(data): 성공 응답 (상태 코드 200)
  • created(data): 리소스 생성 성공 응답 (상태 코드 201)
  • noContent(): 콘텐츠 없는 성공 응답 (상태 코드 204)
  • badRequest(message): 잘못된 요청 오류 (상태 코드 400)
  • unauthorized(message): 인증 실패 오류 (상태 코드 401)
  • forbidden(message): 접근 권한 없음 오류 (상태 코드 403)
  • notFound(message): 리소스를 찾을 수 없음 오류 (상태 코드 404)
  • conflict(message): 충돌 오류 (상태 코드 409)
  • validationError(message): 유효성 검사 오류 (상태 코드 422)
  • systemError(message): 시스템 오류 (상태 코드 500)
  • serviceUnavailable(message): 서비스 사용 불가 오류 (상태 코드 503)

모든 메서드는 일관된 구조의 ResponseDTO 객체를 반환합니다. 데이터가 없는 경우에도 data 필드는 null로 명시적으로 포함됩니다.

응답 구조

모든 ResponseDTO 객체는 다음과 같은 구조를 가집니다:

{
  type: string,    // 'success', 'client', 또는 'system'
  status: number,  // HTTP 상태 코드
  data: any,       // 응답 데이터 (없는 경우 null)
  message?: string // 추가 메시지 (주로 오류에 사용, 옵션)
}

예제

Express.js와 함께 사용하는 예제:

const express = require('express');
const ResponseDTO = require('@ahhaohho/response-dto');

const app = express();

app.get('/api/users/:id', (req, res) => {
  try {
    const user = { id: req.params.id, name: '홍길동' };
    res.json(ResponseDTO.success(user));
  } catch (error) {
    res.status(500).json(ResponseDTO.systemError('사용자 정보를 가져오는 데 실패했습니다.'));
  }
});

app.post('/api/users', (req, res) => {
  // 새 사용자 생성 로직
  const newUser = { id: 123, name: '새로운 사용자' };
  res.status(201).json(ResponseDTO.created(newUser));
});

app.delete('/api/users/:id', (req, res) => {
  // 사용자 삭제 로직
  res.json(ResponseDTO.noContent());
});

app.listen(3000, () => console.log('서버가 3000번 포트에서 실행 중입니다.'));

타입 안전성

ResponseDTO는 모든 응답에 data 필드를 포함하며, 데이터가 없는 경우에도 null을 명시적으로 설정합니다. 이는 타입스크립트나 다른 정적 타입 언어를 사용하는 클라이언트에서 타입 안전성을 보장하는 데 도움이 됩니다.

작성자

정두수 ([email protected])


이 README는 프로젝트의 기본적인 정보를 제공합니다. 필요에 따라 내용을 수정하거나 확장할 수 있습니다.