Skip to content

Instantly share code, notes, and snippets.

View nicowernli's full-sized avatar

Nicolás Wernli nicowernli

View GitHub Profile
@nicowernli
nicowernli / http-error.interceptor.ts
Last active March 28, 2019 11:28
An error interceptor to manage all HTTP errors in a single way
import {Injectable} from '@angular/core';
import {HttpHandler, HttpRequest, HttpInterceptor} from '@angular/common/http';
import {throwError} from 'rxjs';
import {catchError} from 'rxjs/internal/operators';
import {ErrorService} from '../my-services/error.service';
@Injectable({
providedIn: 'root'
})
export class HttpErrorInterceptor implements HttpInterceptor {
@nicowernli
nicowernli / base-url.interceptor.ts
Last active March 28, 2019 11:39
Simple HttpInterceptor that adds a header to a request
import { Injectable } from '@angular/core';
import {HttpHandler, HttpRequest, HttpInterceptor} from '@angular/common/http';
import {environment} from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class BaseURLInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (!req.url.match(/^http(s)?:\/\/(.*)$/)) {
// GetItem finds a returns a single item from a dynamodb table.
// It returns an element and an error if any.
// If no element is found, will return nil without an error
func GetItem(id string, dest interface{}) error {
tableName := "table-name"
// create get operation information
item := &dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
@nicowernli
nicowernli / search-bar.component.ts
Created August 31, 2018 10:16
Debounce time included
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { switchMap, debounceTime } from 'rxjs/operators';
import { PlanetService } from '../planet.service';
import { Planet } from '../planet';
@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html'
@nicowernli
nicowernli / search-bar.component.html
Created August 31, 2018 09:58
Search bar component template
<div class="p-4">
<p class="mb-2">search-bar works!</p>
<div class="relative">
<input [formControl]="search" type="text" class="border border-grey rounded shadow-inner p-2 w-full" placeholder="Search...">
<div class="absolute mt-1 bg-grey w-full rounded shadow">
<ul class="list-reset">
<li *ngFor="let planet of (planets$ | async)" class="p-2">{{ planet.name }}</li>
</ul>
</div>
@nicowernli
nicowernli / searc-bar.component.ts
Created August 31, 2018 09:54
The Search Bar Component base
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { PlanetService } from '../planet.service';
import { Planet } from '../planet';
@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html'
@nicowernli
nicowernli / app.module.ts
Created August 31, 2018 07:52
App Module with ReactiveForms
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { SearchBarComponent } from './search-bar/search-bar.component';
import { PlanetService } from './planet.service';
@NgModule({
@nicowernli
nicowernli / planet.ts
Last active August 31, 2018 10:56
Planet interface
export interface Planet {
name: string;
rotation_period: number;
orbital_period: number;
diameter: number;
climate: string;
gravity: string;
terrain: string;
surface_water: number;
population: number;
@nicowernli
nicowernli / planet.service.ts
Created August 31, 2018 07:43
Planet service
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Planet } from './planet';
@Injectable()
export class PlanetService {
constructor(private http: HttpClient) {}
@nicowernli
nicowernli / rxjs-switchMap.js
Created March 23, 2018 11:53
Medium switchMap
const Rx = require('rxjs');
const long$ = Rx.Observable.interval(1000).take(4);
const short$ = Rx.Observable.interval(500).take(4);
long$
.switchMap(long => short$.map(short => console.log({ long, short })))
.subscribe();
/** Output