Coder Social home page Coder Social logo

typesafe-url-generator's Introduction

Typesafe URL Generator

Package for creating typesafe urls

Getting started

You can install this package via running npm install typesafe-url-generator in your local application directory

Example Usage

// apiConfig.ts
import { createURLGenerator, type URLGeneratorConfig } from 'typesafe-url-generator';

const config = {
  products: {
    environments: {
      local: 'http://localhost:3000',
      development: 'https://dev',
      production: 'https://prod',
    },
    requests: {
      getProducts: '/products',
      getProduct: '/products/:id',
      getProductsByCategory: '/products/category?page=&limit=10',
    },
  },
} as const satisfies URLGeneratorConfig;

export const { generateUrl } = createURLGenerator(
  config,
  'development'
);

// someRequestFile.ts
import { generateUrl } from './apiConfig.ts'

const requestWithParams = generateUrl({
  api: 'products',
  request: 'getProducts',
});

const requestWithParams = generateUrl({
  api: 'products',
  request: 'getProduct',
  replace: {
    id: '1337', // Required
  },
});

const requestWithFilters = generateUrl({
  api: 'products',
  request: 'getProductsByCategory',
  // Optional
  filters: {
    page: '1', // Will be removed from the url if not set
    // limit: '10' Will default to 10 as defined in the config
  },
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.