Socket
Socket
Sign inDemoInstall

agent-request

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    agent-request

request agent base on fetch


Version published
Maintainers
1
Install size
59.3 kB
Created

Readme

Source

agent

http agent base on origin fetch

TODO

  • race condition avoid
  • cache management

example

const createAgent = <T, U>(agentInit?: AgentInit<T, U>): Fetch => {
  const agent = new Agent(window.fetch.bind(window), agentInit);

  agent.interceptors.request.use(
    init => {
      if (!init.credentials) init.credentials = 'include';
      if (!init.contentType) init.contentType = ContentType.JSON;
      if (!init.responseType) init.responseType = ContentType.JSON;

      init.headers = {
        ...init.headers,
        'X-TEST-HEAD': 'x-test-head',
      };
      return init;
    },
    err => {
      return err;
    }
  );

  agent.interceptors.response.use(
    res => {
      if (!res.ok) {
        throw new Error(res.statusText ?? `${res.status} error`);
      }
      const data: ResponseVO<unknown> | undefined = res.data as
        | ResponseVO<unknown>
        | undefined;
      if (!data?.success) {
        throw new Error(data?.msg ?? 'Invalid error');
      }
      return res;
    },
    err => {
      return err;
    }
  );

  const fetcher = <T, U>(
    input: string,
    reqInit?: Partial<AgentReqInit<T, U>> & { noErrorHint?: boolean }
  ) => {
    const init: AgentReqInit<T, U> & { noErrorHint?: boolean } = {
      input,
      base: agentInit?.base,
      timeout: agentInit?.timeout,
      ...reqInit,
    };

    return agent
      .request<T, U>(init)
      .then(res => res?.data)
      .catch(err => {
        if (!init?.noErrorHint) {
          notification.error({
            type: 'error',
            message: err.name,
            description: (init.url ?? init.input) + ' ' + err.message,
          });
        }
        throw err;
      });
  };

  return fetcher;
};

export { createAgent };

export default createAgent<unknown, unknown>({
  base: '/test_api_base_prefix',
  timeout: 30000,
  queue: { concurrency: 5 },
  retry: {
    delay: 100,
    maxTimes: 3,
    retryOn: (_, err) => !!err,
  },
});

Keywords

FAQs

Last updated on 06 Feb 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc