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

jin-frame

v3.13.0

Published

Reusable HTTP API request definition library

Downloads

158

Readme

jin-frame

ts Download Status Github Star Github Issues NPM version License ci codecov code style: prettier

HTTP Reqest = TypeScript Class

jin-frame help to make HTTP Request template using by TypeScript class, decorator.

Why jin-frame?

  1. decorator: decorator function make HTTP request parameter
  2. Static Type Checking: Compile-Time static type checking on request parameter
  3. HTTP Request can extends inheritance, OOP design
  4. Use Axios EcoSystem

Table of Contents

How to works?

jin-frame

Comparison of direct usage and jin-frame

| Direct usage | Jin-Frame | | -------------------------------- | --------------------------------------- | | axios | jin-frame |

Install

npm i jin-frame --save

Useage

Querystring made by query, Q decorator.

class IamReqest extends JinFrame {
  @JinFrame.Q()
  public readonly name!: string;
}

Path parameter made by param, P decorator and URI.

class IamReqest extends JinFrame {
  // decorator
  @JinFrame.P()
  public readonly id!: string;

  constructor(args: OmitConstructorType<IamReqest, JinBuiltInMember>) {
    // `:` character make path parameter on URI
    super({ ...args, host: 'http://some.api.google.com/jinframe/:id', method: 'post' });
  }
}

Header parameter made by header, H decorator and URI.

class IamReqest extends JinFrame {
  @JinFrame.H({ replaceAt: 'api-key' })
  public readonly apiKey!: string;
}

Body parameter made by body, B decorator and URI.

class IamReqest extends JinFrame {
  @JinFrame.B({ replaceAt: 'api-key' })
  public readonly gene!: string;
}

This is example of union param, body, header parameter.

class TestPostFrame extends JinFrame {
  @JinFrame.param()
  public readonly id!: number;

  @JinFrame.body({ replaceAt: 'test.hello.marvel.name' })
  public readonly name!: string;

  @JinFrame.header({ replaceAt: 'test.hello.marvel.skill' })
  public readonly skill!: string;

  // automatically initialize via base class, have to use same name of args and JinFrame class
  // execute `Object.keys(args).forEach(key => this[key] = args[key])`
  constructor(args: OmitConstructorType<TestPostFrame, JinBuiltInMember>) {
    super({ ...args, $$host: 'http://some.api.yanolja.com/jinframe/:id', $$method: 'POST' });
  }
}

TestPostFrame class create AxiosRequestConfig object below. $$ character is show that is built-in variable.

const frame = new TestPostFrame({ id: 1, name: 'ironman', skill: 'beam' });
console.log(frame.request());

// console.log show below,
{
  timeout: 2000,
  headers: { test: { hello: { marvel: { skill: 'beam' } } }, 'Content-Type': 'application/json' },
  method: 'POST',
  data: { test: { hello: { marvel: { name: 'ironman', gender: 'male' } } } },
  transformRequest: undefined,
  url: 'http://some.api.yanolja.com/jinframe/1',
  validateStatus: () => true
}

You can direct execute jin-frame. Curried request function create after execute it. jin-frame using axios library so using on browser.

const frame = new TestPostFrame({ id: 1, name: 'ironman', skill: 'beam' });
const res = await frame.execute();

// or
const resp = await axios.request(frame.request());

Requirements

  1. TypeScript
  2. Decorator
    • enable experimentalDecorators, emitDecoratorMetadata option in tsconfig.json

Axios version

| jin-frame | axios | | --------- | --------- | | 2.x | <= 0.27.x | | 3.x | >= 1.1.x |

Mocking

jin-frame use axios internally. So you can use axios-mock-adapter.

import axios from 'axios';
import MockAdapter from 'axios-mock-adpater';

// This sets the mock adapter on the default instance
const mock = new MockAdapter(axios);

// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
  users: [{ id: 1, name: 'John Smith' }],
});

const frame = new UserFrame({ params: { searchText: 'John' } });
const reply = await frame.execute();

console.log(response.data);

Form

The form data is multipart/form-data and application/x-www-form-urlencoded. Use to upload files or submit form fields data.

application/x-www-form-urlencoded

application/x-www-form-urlencoded converts from data using the trasformRequest function in axios. For jin-frame, if you set the application/x-www-form-urlencoded to content-type, use the built-in transformRequest function or pass transformRequest function to constructor.

multipart/form-data

jin-frame uses the form-data package for form-data processing. If you set the multipart/form-data content-type, use the form-data package to generate the AxiosRequestConfig data field value. Alternatively, upload the file by passing the customBody constructor parameter.

Hook

JinFrame support pre, post hook side of each request.

class TestPostFrame extends JinFrame {
  @JinFrame.param()
  public readonly id!: number;

  @JinFrame.body({ replaceAt: 'test.hello.marvel.name' })
  public readonly name!: string;

  @JinFrame.header({ replaceAt: 'test.hello.marvel.skill' })
  public readonly skill!: string;

  override preHook(req: AxiosRequestConfig<unknown>): void {
    console.log('pre hook executed');
  }

  override postHook(req: AxiosRequestConfig<unknown>): void {
    console.log('post hook executed');
  }

  // automatically initialize via base class, have to use same name of args and JinFrame class
  // execute `Object.keys(args).forEach(key => this[key] = args[key])`
  constructor(args: OmitConstructorType<TestPostFrame, JinBuiltInMember>) {
    super({ ...args, host: 'http://some.api.google.com/jinframe/:id', method: 'POST' });
  }
}

const frame = new TestPostFrame({ id: 1, name: 'ironman', skill: 'beam' });

// 'pre hook executed' display console
const res = await frame.execute();
// 'post hook executed' display console

Field for logging, debugging

query, header, param, body getter function have each request parameter.

const frame = new TestPostFrame({ id: 1, name: 'ironman', skill: 'beam' });
// jin-frame build body, header, query, param variable
const res = await frame.execute();

// You can verify body, header, query parameter
console.log(frame.body);
console.log(frame.header);
console.log(frame.query);
console.log(frame.param);

Example

You can find more examples in examples directory.

License

This software is licensed under the MIT.