Security News
cURL Project and Go Security Teams Reject CVSS as Broken
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
url-sweatshirt
Advanced tools
Wrap your URLs in a warm layer of helper functions.
Projects like JsRoutes are great when your JavaScript is tightly integrated
with a Rails backend. However, if you want to reduce coupling between your
frontend and backend code, you're going to need to manage your routes manually.
url-sweatshirt
eases that transition by generating Rails-like URL helpers for
you.
var generate = require('url-sweatshirt').generate;
var userPostUrl = generate('/users/:userId/posts/:id');
// all return '/users/1/posts/2'
userPostUrl(1, 2));
userPostUrl(1, { id: 2 });
userPostUrl({ userId: 1, id: 2 });
// returns '/users/1/posts/2?q=javascript'
userPostUrl(1, 2, { q: 'javascript' });
// returns '/users/1/posts/2?q=javascript&foo=bar'
userPostUrl(1, 2, { q: 'javascript' }, { foo: 'bar' });
When you define a URL, you can pass in defaults for both path parameters and query parameters.
var categoryUrl = generate('/categories/:name', { name: 'all', locale: 'en' });
// returns '/categories/all?locale=en'
categoryUrl();
Callers can then override the defaults. They can also remove a default query
parameter by passing null
.
// returns '/categories/books'
categoryUrl('books', { locale: null });
_anchor
, _host
, and _protocol
are special:
var fancyUrl = generate('/', {
_host: 'www.example.com',
_protocol: 'https'
});
// returns 'https://www.example.com/#post-5'
fancyUrl({ _anchor: 'post-5' });
If you provide _host
but not _protocol
, you'll get a protocol-relative URL
(i.e., one starting with //
).
If you need to define a bunch of helpers with shared default parameters, you
can use the withDefaults
function. It takes a callback and passes in a
version of generate
with the given defaults baked in.
var withDefaults = require('url-sweatshirt').withDefaults;
var urls = {};
withDefaults({ _host: 'api.example.com' }, function(generate) {
urls.userUrl = generate('/users/:id');
});
// returns '//api.example.com/users/1'
urls.userUrl(1);
By default, the query string is built using a simple function that calls
toString()
on param values and then URI-encodes the param keys and values.
For some use cases, it's helpful to use alternate strategies for this -- for
example, you might want to use jQuery's $.param
to convert objects and arrays
into Rails-style bracket notation. You can accomplish this by calling the
function exported from the url-sweatshirt
module and passing in a function
with the appropriate signature (takes a single object param and returns a
string).
var simpleGenerate = require('url-sweatshirt').generate;
var simpleHomeUrl = simpleGenerate('/');
var complexGenerate = require('url-sweatshirt')($.param).generate;
var complexHomeUrl = complexGenerate('/');
// returns '/?a=1&b=[object%20Object]'
simpleHomeUrl({ a: 1, b: { c: 2, d: 3 }});
// returns '/?a=1&b[c]=2&b[d]=3`
complexHomeUrl({ a: 1, b: { c: 2, d: 3 }});
FAQs
Wrap your URLs in a warm layer of helper functions
The npm package url-sweatshirt receives a total of 15 weekly downloads. As such, url-sweatshirt popularity was classified as not popular.
We found that url-sweatshirt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
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.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.