UNPKG

angular-kladr

Version:

Angular 4+ package for better experience using kladr api service

191 lines (179 loc) 5 kB
import { CommonModule } from '@angular/common'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientJsonpModule, HttpClientModule } from '@angular/common/http'; import { Jsonp, JsonpModule } from '@angular/http'; import { map } from 'rxjs/operators'; import { InjectionToken, Injectable, Inject, NgModule, defineInjectable, inject } from '@angular/core'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** @enum {string} */ const KladrContentType = { region: 'region', district: 'district', city: 'city', street: 'street', building: 'building', }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** @enum {number} */ const KladrWithParent = { /** * Выдача с родительскими объектами * @type {number} */ with: 1, /** * Выдача без родительских объектов * @type {number} */ without: 0, }; KladrWithParent[KladrWithParent.with] = 'with'; KladrWithParent[KladrWithParent.without] = 'without'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** @enum {number} */ const KladrCodeType = { /** * города * @type {number} */ cities: 1, /** * поселки * @type {number} */ townships: 2, /** * деревни * @type {number} */ villages: 4, }; KladrCodeType[KladrCodeType.cities] = 'cities'; KladrCodeType[KladrCodeType.townships] = 'townships'; KladrCodeType[KladrCodeType.villages] = 'villages'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** @enum {number} */ const KladrOneString = { true: 1, false: 0, }; KladrOneString[KladrOneString.true] = 'true'; KladrOneString[KladrOneString.false] = 'false'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** @type {?} */ const KLADR_OPTIONS = new InjectionToken('KLADR_OPTIONS'); /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class Options { constructor() { this.isSecure = false; } } /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class KladrService { /** * @param {?} jsonp * @param {?} options */ constructor(jsonp, options) { this.jsonp = jsonp; this.options = options; this.baseUrl = 'kladr-api.ru/api.php'; this.HTTP = 'http://'; this.HTTPS = 'https://'; this.url = options.isSecure ? this.HTTPS + this.baseUrl : this.HTTP + this.baseUrl; } /** * Makes api request * @param {?} query of type KladrRequestParams * @return {?} Observable<KladrResponse> Observable with received data or error message */ api(query) { /** @type {?} */ let apiUrl = this.url + '?'; Object.keys(query).forEach(key => { apiUrl += `${key}=${query[key]}&`; }); apiUrl += 'callback=JSONP_CALLBACK'; return this.jsonp.request(apiUrl).pipe(map((response) => { return (/** @type {?} */ (response.json())) || response.json(); })); } } KladrService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ KladrService.ctorParameters = () => [ { type: Jsonp }, { type: Options, decorators: [{ type: Inject, args: [KLADR_OPTIONS,] }] } ]; /** @nocollapse */ KladrService.ngInjectableDef = defineInjectable({ factory: function KladrService_Factory() { return new KladrService(inject(Jsonp), inject(KLADR_OPTIONS)); }, token: KladrService, providedIn: "root" }); /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class KladrModule { /** * @param {?=} options * @return {?} */ static forRoot(options = undefined) { return { providers: [ { provide: KLADR_OPTIONS, useValue: options } ], ngModule: KladrModule }; } } KladrModule.decorators = [ { type: NgModule, args: [{ declarations: [], imports: [ CommonModule, BrowserModule, JsonpModule, HttpClientJsonpModule, HttpClientModule ], providers: [ KladrService ] },] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ export { KladrContentType, KladrWithParent, KladrCodeType, KladrOneString, KladrModule, KladrService, Options, KLADR_OPTIONS }; //# sourceMappingURL=angular-kladr.js.map