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

response-type

v1.1.5

Published

This repository is a type definition based on google json styleguide. You will have some interfaces that help you to determine what your response will look like.

Downloads

11

Readme

response-type: Google JSON StyleGuide tools and definition for JS/TS

A set of definitions for google JSON StyleGuide to unify the API responses. This library has those definitions in typescript, and allows you to use some helper functions such as IsSuccess and IsFailure to detect the responses from server.

https://google.github.io/styleguide/jsoncstyleguide.xml

Installing

You can install this library quite easily:

npm install response-type --save

or using yarn:

yarn add response-type

Using response-type

Just import it into your application via import statement:

import { IResponse, IResponseErrorItem, /* rest of things you need */ } from 'response-type';

List of definitions

We implement exact what google defines. For understanding this schema, read google documentation:

https://google.github.io/styleguide/jsoncstyleguide.xml

IResponse

Defines the entire body of the response. If a JSON coming from server, it should match this schema.

export interface IResponse<T> {
  apiVersion?: string;
  context?: string;
  id?: string;
  params?: {
    id?: string;
  };
  data?: IResponseData<T>;
  error?: IResponseError;
}

IResponseData

Defines the success part of the API response as data.

export interface IResponseData<T> {
  kind?: string;
  fields?: string;
  etag?: string;
  id?: string;
  lang?: string;
  updated?: string;
  deleted?: boolean;
  currentItemCount?: Number;
  itemsPerPage?: Number;
  startIndex?: Number;
  totalItems?: Number;
  pageIndex?: Number;
  totalPages?: Number;
  items?: Array<T>;
  [key: string]: any;
}

IResponseError

Defines the failure part of the API response, and contains the error message, and a list of sub errors (for example related to an specific input)

export interface IResponseError {
  code?: Number;
  message: string;
  errors?: Array<IResponseErrorItem>;
}

IResponseErrorItem

Defines an error particle inside the error object.

export interface IResponseErrorItem {
  domain?: string;
  reason?: string;
  message?: string;
  location?: string;
  locationType?: string;
  extendedHelp?: string;
  sendReport?: string;
}

Functions

IsSuccess

You can check if a response was successful using this function.

import { IsSuccess } from 'response-type';

const response = {
  data: {
    fields: 'name,phone',
    name: 'ali',
    phone: '+989199493941'
  }
}

console.log(IsSuccess(response));

// true

IsFailure

You can check if a response was failed due to some reasons.

import { IsFailure } from 'response-type';

const response = {
  error: {
    code: 0
    message: 'Unknown error',
    errors: [
      {
        location: 'name',
        message: 'Name is very wrong'
      }
    ]
  }
}

console.log(IsFailure(response));
// true

FieldError

Finds if response has error message for specific field. Helpful for the form validation, and showing the error messages.

import { FieldError } from 'response-type';

const response = {
  error: {
    code: 0
    message: 'Unknown error',
    errors: [
      {
        location: 'name',
        message: 'Name is very wrong'
      }
    ]
  }
}

console.log(FieldError(response, 'name));
// 'Name is very wrong'

Credits

Implemented by Ali Torabi.