@ilb/js-auto-proxy

2.0.0 • Public • Published

Js auto proxy

Installing

$ npm install --save @ilb/js-auto-proxy

Usage

  1. Creating proxy from class instance
import { createJsProxy } from '@ilb/js-auto-proxy';

const api = new Api(); // your class instance
const proxyApi = createJsProxy(api, 'apipath');

Note: second argument is a string to configure your api path (see below)

  1. Configure api (for client requests)

Create file pages/api/{apipath}.js, where apipath - string from above (can be either a single file name or a string with folders). For example pages/api/dossier.js. Write in this file:

import { executeApi } from '@ilb/js-auto-proxy';

export default async (req, res) => {
  const api = new Api(); // your class instance
  executeApi(api, req, res);
};

And that's all!!!

You can use your created proxyApi everywhere and don't bother about server's or client's troubles. Just run it like an ordinary method:

const { response, error } = await proxyApi.getSomeCoolStuff(arg1, arg2, ...);

One more setup if you wanna operate with files

Create file pages/api/{apipath}_multipart.js, where apipath - string from above. For example pages/api/dossier_multipart.js Write in this file:

import { executeFileApi } from '@ilb/js-auto-proxy';

export default async (req, res) => {
  const api = new Api(); // your class instance
  executeFileApi(api, req, res);
};

export const config = {
  api: {
    bodyParser: false,
  },
};

Note: here we switching off nextjs bodyParser and parse responses by yourself (don't worry, I did it all for you)


The way to pass (the same) params to constructor when creating instance in api folder

Some time your class instances need to be initialized so you must pass same params to constructor in api folder too. To do this you mus first create method getQuery in your class that will return params from client side class instance:

export default class Repository {
  constructor ({ param1, param2, param3 }) {
    this.param1 = param1;
    this.param2 = param2;
    this.param3 = param3;
  }

  getQuery = () => ({
    param1: this.param1,
    param2: this.param2,
    param3: this.param3,
  });

  ...
}

Then when creating instance of this class on the server side (in api folder) pass req.query in it (js-auto-proxy will pass it through):

export default async (req, res) => {
  const repos = new Repository(req.query);
  executeApi(repos, req, res);
};

TODO docs for ApiClient
Suppot to handle async requests in ApiClient: pass async: true to request options:
let apiClient = new ApiClient();
apiClient = createJsProxy(apiClient, 'proxy'); // for client side
const result = apiClient.get(urlToAsyncService, { async: true });
TODO correct transpile async/await syntax

Readme

Keywords

none

Package Sidebar

Install

npm i @ilb/js-auto-proxy

Weekly Downloads

2

Version

2.0.0

License

none

Unpacked Size

40.5 kB

Total Files

8

Last publish

Collaborators

  • barakok
  • dmitryi698
  • ssortia
  • unsfer
  • slavb18
  • maximumquiet