Skip to content

Instantly share code, notes, and snippets.

@ssuperczynski
Last active November 6, 2018 08:09
Embed
What would you like to do?
export interface Recruitment {
id?: number;
name: string;
type: number;
}
export interface ApiEndpoint {
'/v1/api/recruitments': { params: null, response: Recruitment[]};
'/v1/api/recruitments/:id': { params: [number], response: Recruitment};
}
@Injectable()
export class RecruitmentService {
fetch<E extends keyof ApiEndpoint>(endpoint: E, ...args: ApiEndpoint[E]['params'][]): Observable<ApiEndpoint[E]['response']> {
return this.http.get<ApiEndpoint[E]['response']>(`${endpoint}${args}`);
}
}
export class ListComponent implements OnInit {
public recruitments: Recruitment[];
public recruitment: Recruitment;
ngOnInit() {
const id = 1;
this.recruitmentService.fetch('/v1/api/recruitments').subscribe((response) => this.recruitments = response);
this.recruitmentService.fetch('/v1/api/recruitments', id).subscribe((response) => this.recruitment = response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment