
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
http-service-ts
Advanced tools
This package is available in npm. Install it in your project with the following command:
npm i http-service-ts
CommonJS
const { RequestParser, Service } = require('http-service-ts');
ES6
import { RequestParser, Service } from 'http-service-ts';
RequestParser classAllows to make requests and get their responses formatted. Returns new promise with content as JSON, text (string), Blob or null.
Basic usage:
interface IP {
ip: string;
}
const http = new RequestParser();
http.request<IP>({
url: 'https://api6.ipify.org?format=json',
method: 'get'
})
.then(
(response) => console.log(response.ip),
(error) => console.error('Error! Server respond with: ', error)
);
You can optionally provide headers in every request...
...
method: 'get',
headers: new Headers(),
...
...or use fixed headers for all requests:
http.config.headers.append('Authorization', token);
You can provide a root in constructor. So when you give an url to request, it will be concatenated with the root. As follows:
const usersApi = new RequestParser('https://api.example.com');
const promise = usersApi.get<User>({
url: 'users',
method: 'get',
id: 1
}); // Will fetch https://api.example.com/users/1
Note that you don't need to put a slash (/) before the URI. It's optional.
config.appendSlashIf server only supports requests with a / at the final of the URL, set appendSlash property to true in configurations:
const api = new RequestParser('https://api.example.com');
api.config.appendSlash = true;
api.request<User>({
url: 'users',
method: 'get',
id: 1
}); // Will fetch https://api.example.com/users/1/
There is another way to set configurations for every request. You can pass them in constructor, as a second argument:
const api = new RequestParser('https://api.example.com', {
headers: new Headers({
Accept: 'application/json'
}),
appendSlash: true
});
Let's create our first example again with a different syntax. You can use search params:
const http = new RequestParser('https://api6.ipify.org');
http.request<IP>({
method: 'get',
params: {
format: 'json'
}
})
.then(
(response) => console.log(response.ip),
(error) => console.error('Error! Server respond with: ', error)
);
// fetch https://api6.ipify.org?format=json
Service classThe methods provided in Service class are: get(), getById(), post(), put(), patch() and delete(). This class extends RequestParser, so request() method is available too.
You can use these methods for managing CRUD operations or write your own class extending Service. Example:
interface Product {
id?: number;
name: string;
price: number;
}
class ProductService extends Service<Product> {
constructor() {
super('https://api.example.com/products');
}
getOffers() {
// GET https://api.example.com/products/offers
return this.request<Product[]>({ url: 'offers', method: 'get' })
}
}
Using in a view:
class HomeView {
products: Product[] = [];
errorMessage: string;
private productService = new ProductService();
constructor() { }
private setProducts(promise: Promise<Product[]>) {
promise
.then(
(products) => this.products = products,
(error) => this.errorMessage = 'There was an error trying to request products!'
);
}
getProducts() {
this.setProducts(this.productService.get());
}
getOffers() {
this.setProducts(this.productService.getOffers());
}
postProduct(product: Product) {
this.productService.post(product)
.then(
(success) => this.getProducts(),
(error) => this.errorMessage = 'There was an error trying to post product!'
);
}
}
When running this package on Node.js you can get an error because of Headers class.
This can be easily fixed with the following lines:
const fetch = require('node-fetch');
global.fetch = fetch;
global.Headers = fetch.Headers;
FAQs
Customizable service writen in TypeScript
We found that http-service-ts 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
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.