GithubHelp home page GithubHelp logo

Comments (3)

SamanthaAdrichem avatar SamanthaAdrichem commented on June 1, 2024

Hey. My Solution is for AngularJs not for Angular.

For Angular we actually no longer use Restangular. We added a request interceptor to convert all gets to the override posts

import {HttpEvent, HttpHandler, HttpInterceptor, HttpParams, HttpRequest} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {LibString} from 'src/app/core/lib/string';

@Injectable()
export class GetAsPostInterceptor implements HttpInterceptor {

	public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
		if (
			request.url
			&& LibString.startsWith(request.url, '/api')
			&& request.method
			&& request.method.toUpperCase() === 'GET'
		) {
			let body: {[key: string]: any} = {};
			request.params.keys().map((paramName: string) => {
				body[paramName] = request.params.getAll(paramName);
				body[paramName] = body[paramName].length === 1 ? body[paramName].pop() : body[paramName];
			});
			request = request.clone({
				method: 'POST',
				setHeaders: {
					'X-HTTP-Method-Override': request.method
				},
				params: new HttpParams(),
				body: body
			});
		}
		return next.handle(request);
	}
}

and in app.module.ts

import {GetAsPostInterceptor} from '.../get-as-post.interceptor';

@NgModule({
	exports: [
	],
	imports: [
	],
	// All providers you need in AngularJS
	providers: [
		// Request interceptors, might be able to move them to the actual modules, but those are generated
		{ provide: HTTP_INTERCEPTORS, useClass: GetAsPostInterceptor, multi: true },
	]
})
export class AppModule implements DoBootstrap {
}

LibString

export class LibString {

	public static startsWith(inputString: string, prefix: string): boolean {
		return inputString.indexOf(prefix) === 0;
	}
}

(edit) Only bug it has now is that if you add an array of [0] it's perceived as empty 🤷‍♀ didn't have time to fix it.

from ngx-restangular.

chander avatar chander commented on June 1, 2024

To be clear, ngx-Restangular is an angular component, and I believe that the change you made to Angular's Restangular is relevant here (thanks for that!)

The fact that your application doesn't use ngx-restangular (which is the Angular version of Restangular) is not really relevant - others do use ngx-restangular, and the fix/change is relevant to them.

thanks

from ngx-restangular.

SamanthaAdrichem avatar SamanthaAdrichem commented on June 1, 2024

Yeah, I understand, but as I said my pull request is on the AngularJS version. Hope they will implement it for you in the Angular version

from ngx-restangular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.