Skip to content

Instantly share code, notes, and snippets.

@Dornhoth
Dornhoth / persistence.service.ts
Last active June 7, 2020 10:55
Persistence Service with Injection Token for Storage
import { Injectable, InjectionToken, Inject } from '@angular/core';
export const STORAGE = new InjectionToken<Storage>('Storage', {
providedIn: 'root',
factory: () => localStorage
});
@Injectable({
providedIn: 'root',
})
@Dornhoth
Dornhoth / persistence.service.ts
Last active June 7, 2020 13:20
Persistence Service directly accessing local storage
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class PersistenceService {
set(key: string, data: any): void {
localStorage.setItem(key, JSON.stringify(data));
}
{
"/api": {
"target": "http://localhost:3000/api",
"secure": false
}
}
@Dornhoth
Dornhoth / app.ts
Last active November 26, 2019 16:05
import express from 'express';
import cookieParser from 'cookie-parser';
import csurf from 'csurf';
const app = express();
const csrfProtection = csurf({
cookie: true,
ignoreMethods: ['GET', 'HEAD', 'OPTIONS'],
});
app.use(cookieParser());
@Dornhoth
Dornhoth / package.json
Created November 26, 2019 16:09
start with proxy
"start": "ng serve --proxy-config proxy.conf.json",
import { Component } from '@angular/core';
import { STORAGE, PersistenceService } from '../services/persistence.service';
@Component({
selector: 'app-storage',
templateUrl: './storage.component.html',
providers: [
PerstistenceService,
{ provide: STORAGE, useValue: sessionStorage }
]
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'App';
}
class Greeter implements Person {
protected person: Person;
constructor(person: Person) {
this.person = person;
}
public greet(): string {
return this.person.greet();
}
const hiGreeterStudent = new Student();
const helloGreeterStudent = new HelloGreeter(new Student());
const goodMorningGreeterTeacher = new GoodMorningGreeter(new Teacher());
hiGreeterStudent.greet(); //"Hi!"
helloGreeterStudent.greet(); //"Hello!"
goodMorningGreeterTeacher.greet(); //"Good morning!"
interface Person {
greet(): string;
}
class Student implements Person {
public greet(): string {
return 'Hi!';
}
}