
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@apitizer/endpoint
Advanced tools
The endpoint package of apitizer aims for a chainable and flat api to generate restful api urls in a semantic way. It's designed to follow functional principles and has a high support for generic types.
The endpoint package has a peer dependency on the query package:
yarn add @apitizer/endpoint @apitizer/query
or
npm install @apitizer/endpoint @apitizer/query
An endpoint is created using the endpoint()
factory exported by @apitizer/endpoint.
import { endpoint } from '@apitizer/endpoint';
const api = endpoint('http://myapi.com');
The returned object gives access to several chainable method to create a restful api url in a semantic way. These are namely many
, one
and query
. The actual url can be retrieved by calling get
.
Whenever a method is chained, a new immutable instance of endpoint()
is created.
Produces a deeper nested url pointing to a resource which is intended to return an array of items.
// ...
// Endpoint<User[], UserData>
const users = api.many<User, UserData>('users');
// http://myapi.com/users
const url = users.get();
Produces a deeper nested url pointing to a resource which is intended to return a single resource which is identfiable.
// ...
// Endpoint<User, UserData>
const userWithIdOne = api.one<User, UserData>('users', 1);
// http://myapi.com/users/1
const url = users.get();
Sets query parameters on the current url. If you pass an instance
of a Query
using the query()
factory from @apitizer/query, all previously set query parameters derived from parents are discarded.
// ...
import { query } from '@apitizer/query';
// Always Endpoint<User[], UserData>
const activeUsers = users.query(query().param('active', 'yes'));
const activeUsers = users.query(query({ active: 'yes' }));
const activeUsers = users.query(query().params({ active: 'yes' }));
// http://myapi.com/users?active=yes
const url = activeUsers.get();
However, query parameters from parents can be carried on by passing a callback which will receive the query of the parent to generate a new query:
// ...
// Always Endpoint<User[], UserData>
const activeUsers = users.query(query => query.param('active', 'yes'));
const activeUsersWithFirstName = activeUsers.query(query =>
query.param('firstName', 'John')
);
// http://myapi.com/users?active=yes&firstName=John
const url = activeUsersWithFirstName.get();
To retrieve the actual url string, the get
method is called.
// ...
// http://myapi.com/users?active=yes&firstName=John
const url = activeUsersWithFirstName.get();
// Omit query parameters
// http://myapi.com/users
const url = activeUsersWithFirstName.get(true);
The endpoint
factory can be called with an initial query beside of the initial url.
// ...
const withToken = query({ token: '123' });
const api = endpoint<User, UserData>('http://myapi.com', withToken);
Since it's often required to create deeper api urls based on certain parameters, the template()
method can be used to create a template function based on a factory function to create these.
The parameter typing as the endpoint typing will be transported to the actual template function.
// ...
const getUserCommentsEndpoint = api.template((endpoint, id: number) =>
endpoint.one('users', id).many<Comment, CommentData>('comments')
);
// Endpoint<Comment[], CommentData>
const commentsForUser = getUserCommentsEndpoint(1);
// http://myapi.com/users/1/comments
commentsForUser.get();
FAQs
The Endpoint Builder for the delightful rest api utility
The npm package @apitizer/endpoint receives a total of 1 weekly downloads. As such, @apitizer/endpoint popularity was classified as not popular.
We found that @apitizer/endpoint demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.