Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility: Snake_case to camelCase HTTP interceptor #129

Open
benjamincharity opened this issue Apr 6, 2018 · 0 comments
Open

Utility: Snake_case to camelCase HTTP interceptor #129

benjamincharity opened this issue Apr 6, 2018 · 0 comments
Labels
Focus: utility General JS/TS utilities Type: feature Functionality that doesn't currently exist

Comments

@benjamincharity
Copy link
Contributor

Core currently has:

import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpResponse,
  HttpHandler,
  HttpEvent,
  HttpInterceptor,
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
declare function require(name: string): any;

@Injectable()
export class CamelCaseInterceptor implements HttpInterceptor {

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const camelcaseKeys = require('camelcase-keys');
    return next.handle(request)
      .map((event: HttpEvent<any>) => {
        if (
          event instanceof HttpResponse &&
          (event.headers.get('content-type') || '').indexOf('application/json') > - 1
          && event.body.constructor !== Array
        ) {
          // TODO: Temporarily leaving in both snakecase and adding camelcase keys until all of app is updated
          return event.clone({
            body: Object.assign(
              {},
              event.body,
              camelcaseKeys(event.body, { deep: true, exclude: ['_permissions'] }),
            ),
          });
        }
        return event;
      });
  }
}
@benjamincharity benjamincharity added Focus: utility General JS/TS utilities Type: feature Functionality that doesn't currently exist labels Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Focus: utility General JS/TS utilities Type: feature Functionality that doesn't currently exist
Projects
None yet
Development

No branches or pull requests

1 participant