0.2.0 • Published 5 years ago

generating-url v0.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

Generate Url

install:

npm i generating-url

This module is also compatible with strapi.js APIs

Example usages

Generate an url for making api request:

import url from 'generating-url'

let path = url.generate({
        baseUrl: 'http://localhost:1337', // optional
        path: 'sections', // required field
        query: { // you can define queries according to your API
            _start: 0,
            _limit: 20,
            _where: { // _where query has a special setter for strapi APIs
                id_eq: 5
            },
            _sort: 'id:DESC'
        },
    })
/* path = http://localhost:1337/sections?_start=0&_limit=20&_where[id_eq]=5&_sort=id:DESC */

axios.get(path)
// or
fetch(path) // get

Generate url without query string

let path = url.generate({
    path: 'sections',
    query: false // if you don't want to use the query, you need to pass 'false' here
})
/* path = /sections */

if you dont define the query property, it will use the default query object:

{
    _start: 0,
    _limit: 10,
    _sort: 'id:DESC',
}

Generate url with a parameter and the default query

let path = url.generate({
    path: 'sections/:id',
    params: {
        id: 1
    },
})
/* path = /sections/1?_start=0&_limit=10&_sort=id:DESC */

Generate url with multiple parameter and query

let path = url.generate({
    path: 'sections/:sectionId/task/:taskId',
    params: {
        sectionId: 1,
        taskId: 2
    },
    query: {
        _start: 0,
        _limit: 20,
        _where: {
            id_eq: 5
        },
        _sort: 'id:DESC'
    },
})
/* path = /sections/1/task/2?_start=0&_limit=20&_where[id_eq]=5&_sort=id:DESC */
0.2.0

5 years ago

0.1.8

5 years ago

0.1.9

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago