@lightningkite/ktor-batteries
Version:
50 lines (37 loc) • 2.33 kB
text/typescript
// Package: com.lightningkite.ktordb.live
// Generated by Khrysalis - this file will be overwritten.
import { ReadModelApi } from '../ReadModelApi'
import { HasId } from '../db/HasId'
import { Query } from '../db/Query'
import { UUIDFor } from '../db/UUIDFor'
import { ReifiedType } from '@lightningkite/khrysalis-runtime'
import { HttpBody, HttpClient, fromJSON, unsuccessfulAsError } from '@lightningkite/rxjs-plus'
import { Observable } from 'rxjs'
//! Declares com.lightningkite.ktordb.live.LiveReadModelApi
export class LiveReadModelApi<Model extends HasId<string>> extends ReadModelApi<Model> {
public constructor(public readonly url: string, token: (string | null), headers: Map<string, string> = new Map([]), public readonly serializer: ReifiedType, public readonly querySerializer: ReifiedType) {
super();
this.authHeaders = ((): (Map<string, string> | null) => {
if (token === null || token === undefined) { return null }
return ((it: string): Map<string, string> => (new Map([...headers, ...new Map([["Authorization", `Bearer ${it}`]])])))(token)
})() ?? headers;
}
private readonly authHeaders: Map<string, string>;
public list(query: Query<Model>): Observable<Array<Model>> {
return HttpClient.INSTANCE.call(`${this.url}/query`, HttpClient.INSTANCE.POST, this.authHeaders, HttpBody.json(query), undefined).pipe(unsuccessfulAsError, fromJSON<Array<Model>>([Array, this.serializer]));
}
public get(id: UUIDFor<Model>): Observable<Model> {
return HttpClient.INSTANCE.call(`${this.url}/${id}`, HttpClient.INSTANCE.GET, this.authHeaders, undefined, undefined).pipe(unsuccessfulAsError, fromJSON<Model>(this.serializer));
}
}
export namespace LiveReadModelApi {
//! Declares com.lightningkite.ktordb.live.LiveReadModelApi.Companion
export class Companion {
private constructor() {
}
public static INSTANCE = new Companion();
public create<Model extends HasId<string>>(Model: Array<any>, root: string, path: string, token: (string | null), headers: Map<string, string> = new Map([])): LiveReadModelApi<Model> {
return new LiveReadModelApi<Model>(`${root}${path}`, token, headers, Model, [Query, Model]);
}
}
}