Gathering detailed insights and metrics for ts-sync-request
Gathering detailed insights and metrics for ts-sync-request
Gathering detailed insights and metrics for ts-sync-request
Gathering detailed insights and metrics for ts-sync-request
npm install ts-sync-request
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
21 Commits
1 Forks
2 Watching
1 Branches
1 Contributors
Updated on 30 Dec 2021
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
8.4%
490
Compared to previous day
Last week
2.3%
2,587
Compared to previous week
Last month
-3.2%
11,801
Compared to previous month
Last year
-22.5%
140,902
Compared to previous year
1
1
The package exports SyncRequestClient and SyncRequestService classes which have methods to make synchronous Http GET, POST, PUT, DELETE calls from TypeScript.
ts-sync-request library on npm
TypeScript classes
1class Movie { 2 3 constructor(public name: string, public director: string) { 4 5 } 6} 7 8class Search { 9 10 constructor(public keywords: string) { 11 12 } 13}
Usage
You can use the fluent API by using the SyncRequestClient class as shown below.
1import { SyncRequestClient } from 'ts-sync-request/dist'
get:
1 let id = 1; 2 let url = "http://localhost:59039/api/Movies/" + id; 3 4 let response = new SyncRequestClient() 5 .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4") 6 .get<Movie>(url); 7
post:
1 let url = "http://localhost:59039/api/Movies/search"; 2 3 let search = new Search("Fiction"); 4 5 let response = new SyncRequestClient() 6 .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4") 7 .post<Search, Movie>(url, search); 8
create:
1 let url = "http://localhost:59039/api/Movies"; 2 3 let movie = new Movie("Pulp Fiction", "Quentin Tarantino"); 4 5 let response = new SyncRequestClient() 6 .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4") 7 .create<Movie>(url, movie); 8
put
1 let id = 1; 2 let url = "http://localhost:59039/api/Movies/" + id; 3 4 let movie = new Movie("Pulp Fiction", "Quentin Tarantino"); 5 6 new SyncRequestClient() 7 .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4") 8 .put<Movie>(url, movie);
delete
1 let id = 1; 2 let url = "http://localhost:59039/api/Movies/" + id; 3 4 let response = new SyncRequestClient() 5 .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4") 6 .delete<Movie>(url);
The addHeader API is optional. You can call addHeader multiple times to add multiple headers.
You can use the traditional API by using the SyncRequestService class as shown below.
1import { SyncRequestService, SyncRequestHeader } from 'ts-sync-request/dist';
get:
1 let id = 1; 2 let url = "http://localhost:59039/api/Movies/" + id; 3 4 // Add headers 5 let header = new SyncRequestHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4"); 6 7 let headers: SyncRequestHeader[] = new Array<SyncRequestHeader>(); 8 headers.push(header); 9 10 let response = new SyncRequestService().get<Movie>(url, headers);
The headers parameter is optional.
Below options are supported.
Option | Default | Description |
---|---|---|
followRedirects | true | can be explicitly set to false to prevent following redirects automatically. |
maxRedirects | Infinity | sets the maximum number of redirects. |
timeout | false | times out if no response is returned within the given number of milliseconds. |
retry | false | retry GET requests. Set this to true to retry when the request errors or returns a status code greater than or equal to 400 |
retryDelay | 200 | the delay between retries in milliseconds. |
maxRetries | 5 | the number of times to retry before giving up. |
These options are available via the SyncRequestOptions class. You can set them as you want.
1import { SyncRequestClient, SyncRequestOptions } from 'ts-sync-request/dist'
1 let id = 1; 2 let url = "http://localhost:59039/api/Movies/" + id; 3 4 let options = <SyncRequestOptions> { 5 timeout: true, 6 retry: true 7 }; 8 9 let response = new SyncRequestClient(options) 10 .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDc2OTg1MzgsIm5iZiI6MTU0NzY5NDIxOCwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InN0cmluZyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InN0cmluZyIsIkRPQiI6IjEvMTcvMjAxOSIsImlzcyI6InlvdXIgYXBwIiwiYXVkIjoidGhlIGNsaWVudCBvZiB5b3VyIGFwcCJ9.qxFdcdAVKG2Idcsk_tftnkkyB2vsaQx5py1KSMy3fT4") 11 .get<Movie>(url); 12
No vulnerabilities found.
No security vulnerabilities found.
sync-request
Make synchronous web requests
degenerator
Compiles sync functions into async generator functions
sync-request-curl
Fast way to send synchronous web requests in NodeJS. API is a subset of sync-request. Leverages node-libcurl for high performance. Cannot be used in a browser.
@logtail/types
Better Stack Typescript types (formerly Logtail)