-
Notifications
You must be signed in to change notification settings - Fork 2
/
http-translit-rule-loader.ts
83 lines (70 loc) · 2.49 KB
/
http-translit-rule-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @license
* Copyright DagonMetric. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found under the LICENSE file in the root directory of this source tree.
*/
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable, InjectionToken, Injector, Optional } from '@angular/core';
import { Observable } from 'rxjs';
import { TranslitRuleAny, TranslitRuleLoader } from '@dagonmetric/ng-translit';
/**
* The options for `HttpTranslitRuleLoader`.
*/
export interface HttpTranslitRuleLoaderOptions {
/**
* The base url string or InjectionToken.
*/
baseUrl?: string | InjectionToken<string>;
/**
* The endpoint factory function.
*/
endpointFactory?(baseUrl: string, ruleName: string): string;
}
export const HTTP_TRANSLIT_RULE_LOADER_OPTIONS = new InjectionToken<HttpTranslitRuleLoaderOptions>(
'HttpTranslitRuleLoaderOptions'
);
/**
* Implements an HTTP client API for `TranslitRuleLoader` that relies on the Angular `HttpClient`.
*/
@Injectable({
providedIn: 'any'
})
export class HttpTranslitRuleLoader implements TranslitRuleLoader {
readonly baseUrl: string;
private readonly endpointFactory: (baseUrl: string, ruleName: string) => string;
constructor(
private readonly httpClient: HttpClient,
injector: Injector,
@Optional() @Inject(HTTP_TRANSLIT_RULE_LOADER_OPTIONS) options?: HttpTranslitRuleLoaderOptions
) {
if (options && options.baseUrl) {
if (typeof options.baseUrl === 'string') {
this.baseUrl = options.baseUrl;
} else {
this.baseUrl = injector.get(options.baseUrl);
}
if (!this.baseUrl.endsWith('/')) {
this.baseUrl += '/';
}
} else {
this.baseUrl = '/assets/translit-rules/';
}
if (options && options.endpointFactory) {
// eslint-disable-next-line @typescript-eslint/unbound-method
this.endpointFactory = options.endpointFactory;
} else {
this.endpointFactory = (baseUrl, ruleName) => {
return `${baseUrl}${ruleName}.json`;
};
}
}
getEndpoint(ruleName: string): string {
return this.endpointFactory(this.baseUrl, ruleName);
}
load(ruleName: string): Observable<TranslitRuleAny> {
const endpoint = this.getEndpoint(ruleName);
return this.httpClient.get<TranslitRuleAny>(endpoint);
}
}