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

@ilb/js-auto-proxy

v2.0.0

Published

js auto proxy

Downloads

3

Readme

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