Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

api-base-url-resolver

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-base-url-resolver

Infers a url based on a current url, for instance, allows a front end to resolve a back-end api base url depending on it's running environment. Its very useful when running a lot of environment stages and the front-end must match the back-end environment

latest
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

api base url resolver

Infers a url based on a current url, for instance, allows a front end to resolve a back-end api base url depending on it's running environment.

Its very useful when running a lot of environment stages and the front-end must match the back-end environment

Installation

To install this package use npm install api-base-url-resolver

Usage

subDomainResolver(origin: string, subDomain: string, options?: Options): string;
  • origin : current domain, mostly location.hostname
  • subDomain : the desired domain, mostly api subdomain is used
  • options :
    • replace : replaces the sub-domain to the provided one
    • protocol : changes the protocol
    • conditions : if there some specific condition to a specific env or url, useful for localhost developments envs
    • envVar : when provided, it will look for this env var and use it as url. useful for localhost developments envs or specific deploys like canary or beta

Usage examples

import apiBaseUrlResolver from "api-base-url-resolver";

apiBaseUrlResolver("https://domain.com", "api"); // => "https://api.domain.com"

apiBaseUrlResolver("http://domain.com", "api"); // => "http://api.domain.com"

apiBaseUrlResolver("https://env.domain.com", "api"); // => "https://api.env.domain.com"

apiBaseUrlResolver("https://some.other.app.env.domain.com", "api"); // => "https://api.some.other.app.env.domain.com"

apiBaseUrlResolver("http://domain.com", "api", { protocol: "https" }); // => "https://api.domain.com"

apiBaseUrlResolver("https://domain.com", "api", { protocol: "http" }); // => "http://api.domain.com"

apiBaseUrlResolver("https://app.env.domain.com", "api", { replace: true }); // => "https://api.env.domain.com"

apiBaseUrlResolver("https://app.other.env.domain.com", "api", { replace: true }); // => "https://api.other.env.domain.com"

const conditions = {
    "localhost:8080": "https://custom.env.custom.com",
    "localhost:8081": "http://localhost:8080"
};

subDomainResolver("localhost:8080", "api", { conditions }); // => "https://custom.env.custom.com"
subDomainResolver("localhost:8081", "api", { conditions }); // => "http://localhost:8080"

const envVar = "MAIN_API";
process.env.MAIN_API = "https://env-var-defined-url.com";

subDomainResolver("localhost:8080", "api", { envVar }); // => "https://env-var-defined-url.com"
subDomainResolver("localhost:8081", "api", { envVar }); // => "https://env-var-defined-url.com"
subDomainResolver("https://otherDomain.com", "api", { envVar }); // => "https://env-var-defined-url.com"

Keywords

api

FAQs

Package last updated on 22 Jul 2022

Did you know?

Socket

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