Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
ng-http-sugar
Advanced tools
Syntactic sugar for Angular's built-in HttpClient
Requires Angular 6.0.0 or later.
License: GPL 2.0 or later.
This currently only supports POST
requests. GET
and other types that HttpClient
supports are not yet supported.
Without this, here's what you need to do to make an HTTP POST
request with headers parameters, and callbacks for success and error responses using Angular's included HttpService
:
import { Injectable } from '@angular/core';
import {
HttpClient,
HttpHeaders,
HttpParams
} from '@angular/common/http';
@Injectable()
export class MyService {
constructor(httpService: HttpService) {}
mySuccessCallback(payload: any, responseData: any) {
/* do something with success response */
}
myErrorCallback(payload: any, error: any) {
/* do something with error response */
}
public methodThatMakesARequest(): void {
// API route for your request
let apiRoute = 'https://example.com/api/endpoint';
// Payload to send with your request
let myPayload = { /* payload */ };
// Prepare request headers
myRequestHeaders: Array<{name: string, value: string}> = [
/* add request headers here, e.g.: */
{name: 'SomeHeader', value: 'SomeValue'},
];
let headerArg = new HttpHeaders();
headers.forEach((header) => {
headerArg = headerArg.set(header.name,header.value);
});
// Include default 'content-type' header
// (This is included by HttpService in the next example)
if (! headerArg.has('Content-Type')) {
headerArg = headerArg.set('Content-Type','application/json');
}
// Prepare request params
myRequestParams: Array<{id: string, value: string}> = [
/* add request params here, e.g.: */
{id: 'SomeParam', value: 'SomeValue'},
];
let paramsArg = new HttpParams();
params.forEach((header) => {
paramsArg = paramsArg.set(params.id,params.value);
});
this.httpService.post(
apiRoute,
myPayload,
{
headers: headerArg,
params: paramsArg,
}
).subscribe(
data => {
mySuccessCallback(myPayload,data);
},
error => {
myErrorCallback(myPayload,error);
}
);
}
}
With HttpService
, you can make the same request much more compactly:
import { Injectable } from '@angular/core';
import { HttpService } from 'ng-http-sugar';
@Injectable()
export class MyService {
constructor(private _http: HttpService) {}
mySuccessCallback(payload: any, responseData: any) {
/* do something with success response */
}
myErrorCallback(payload: any, error: any) {
/* do something with error response */
}
public methodThatMakesARequest(): void {
// API route for your request
let apiRoute = 'https://example.com/api/endpoint';
// Payload to send with your request
let myPayload = { /* payload */ };
// Optional array of request header name/value objects
myRequestHeaders: Array<{name: string, value: string}> = [
/* add request headers here, e.g.: */
{name: 'SomeHeader', value: 'SomeValue'},
];
// Optional array of request param id/value objects
myRequestParams: Array<{id: string, value: string}> = [
/* add request params here, e.g.: */
{id: 'SomeParam', value: 'SomeValue'},
];
// This handles preparing HttpHeaders and HttpParams for you, as well
// as subscribing to the response observable to fire callbacks needed.
this._http.post(
apiRoute,
myPayload,
myRequestHeaders, // optional
myRequestParams, // optional
this.mySuccessCallback, // optional
this.myErrorCallback, // optional
);
}
}
HttpSugar
is this module. Include it in your Angular application to use its functionality.HttpService
is an injectable Angular service you can use in your own services, in place of HttpClient
and its helper types.npm install --save 'ng-http-sugar'
.HttpSugarModule
into your application's root module from ng-http-sugar
import { HttpService } from 'ng-http-sugar';
GET
requestsPUT
requestsPATCH
requestsDELETE
requests4.x
and later, instead of 6.x
and later?
4.x
to 6.x
isn't terribly onerous in most cases, so I'll only work on this if I get requests for it.Run ng build http-sugar --prod
to build the library. The build artifacts will be stored in the dist/
directory. Use the --prod
flag for a production build.
# manually increment version in package.json
# avoid git tags since we're publishing from `dist/` later
npm --no-git-tag-version -f version [major|minor|patch]
# build
ng build http-sugar --prod
# switch to built package
cd dist/http-sugar/
# publish update npm
npm publish
There currently are no tests!
ng test
to execute the unit tests via Karma.ng e2e
to execute the end-to-end tests via Protractor.FAQs
Syntactic sugar for Angular's built-in HttpClient
The npm package ng-http-sugar receives a total of 0 weekly downloads. As such, ng-http-sugar popularity was classified as not popular.
We found that ng-http-sugar 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.