npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ezzabuzaid/ngx-request-options

v0.3.0

Published

Pass custom options to http interceptors

Downloads

58

Readme

Request Options

PRs Welcome Downloads per month Version License

An elegant way to pass custom options to interceptor from http client

In a most of the projects, you'll have default URL for your API's gateway that prefixed before sending the request to the backend to avoid adding it to every time and for some reasons you may have a request that doesn't need the default URL, so in this case, you need a way to not prefixing the URL.

That's just one case, you may also have to not send the Authentication header with a request

That's exactly the purpose of this library, is to pass custom options alongside the request and perform specific logic depend on it.

installation

npm install @ezzabuzaid/ngx-request-options

Usage

The library was designed to be added without further modification, you'll still use the same HttpClient but with one additional augment the configure method that takes the default options before choosing the HTTP method.

  1. First of all you need to create you custom options object
interface CustomOptions {
	defaultUrl:boolean;
	defaultAuth: boolean;
}
  1. in app.module you need to import RequestOptionsModule and add it to imports list in NgModule
import { RequestOptionsModule } from  '@ezzabuzaid/ngx-request-options';

@NgModule({
	imports: [
		HttpClientModule,
		RequestOptionsModule.forRoot<CustomOptions>({
			// Default options to be applied on all requests
			defaultAuth: true;
			defaultUrl: true
		})
		]
})

  
// Add those lines as they are
declare module '@angular/common/http/http' {
	// Augment HttpClient with the added `configure` method
	export  interface  HttpClient {
		/**
		* Configure request options.
		*/
		configure(options: Partial<CustomOptions>): HttpClient;
	}
}
  1. Inject HttpClient from @angular/common/http in a class then call the new configure method
@Injectable()
export class MyService {
	constructor(private http: HttpClient) { }
	getData() {
		return this.http
			.configure({ defaultUrl:  false })
			.get('endpoint');
	}
}
  1. into an interceptor
import { RequestOptions } from '@ezzabuzaid/ngx-request-options';
@Injectable()
export class UrlInterceptor implements HttpInterceptor {
	constructor(private requestOptions: RequestOptions<CustomOptions>) { }
	intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
		let  url = request.url;
		if (this.requestOptions.get(request, 'defaultUrl')) {
			url = environment.endpointUrl + request.url;
		}
		return  next.handle(this.requestOptions.clone(request, { url }));
	}
}

Api's

  • RequestOptions
    1. get(request: HttpRequest<any>, option: keyof T)
      • Get an option from the options that was assigned to the request
    2. set(request: HttpRequest<any>, data: Partial<T>)
      • Assign an options to a request
    3. delete(request: HttpRequest<any>)
      • Delete the request options
    4. clone(request: HttpRequest<any>, requestMetadata)
      • Clone the request with new metadata and reassign the options to it
    5. changeRequest(oldRequest: HttpRequest<any>, newRequest: HttpRequest<any>)
      • Sometimes you need to call request.clone() to assign new values to request payload aka metadata thus you need to reassign the options again to the cloned request otherwise the options will be lost. call RequestOptions.clone() instead as shorter version
      • oldRequest the previously used request
      • newRequest the cloned request

Developer

Ezzabuzaid

License

The MIT License (MIT)