{"version":3,"sources":["../src/services/RouteGateway.ts"],"sourcesContent":["import { Api } from './Api';\nimport { ApiGateway } from './ApiGateway';\nimport { Func } from '../types/Func';\nimport { Uri, UriExpandProps } from '../types/Uri';\nimport { PageList } from '../types/PageList';\nimport { Json, JsonValue } from '../types/Json';\nimport { Id } from '../types/Id';\nimport { Optional } from '../types/Types';\nimport { List } from '../types/List';\nimport { toArray } from '../types/Array';\nimport { HttpStatus } from '../http/HttpStatus';\nimport { isDefined } from '../types/Is';\n\nexport class RouteGateway<Props extends UriExpandProps = UriExpandProps> extends ApiGateway {\n  constructor(\n    readonly route: Func<Uri<Props>>,\n    readonly routeId: Func<Uri<Props>>,\n    readonly api: Api = new Api()\n  ) {\n    super(api);\n  }\n\n  all(options?: Props): Promise<PageList<Json>> {\n    return this.get(this.route().expand(options ?? {}) as Uri, options);\n  }\n\n  byId(id: Id): Promise<Optional<Json>> {\n    return this.getOne(this.routeId().id(id) as Uri);\n  }\n\n  byIds(...ids: Id[]): Promise<List<Json>> {\n    return this.get(this.route().ids(toArray(...ids)) as Uri);\n  }\n\n  search(q: JsonValue, options?: Props): Promise<PageList<Json>> {\n    return this.get(this.route().expand({ ...options, q } as Props) as Uri);\n  }\n\n  exists(id: Id): Promise<boolean> {\n    return this.byId(id)\n      .then(r => isDefined(r))\n      .catch(r => (HttpStatus.NotFound.equals(r.status) ? false : Promise.reject(r)));\n  }\n\n  add(item: Json): Promise<Json> {\n    return this.post(this.route() as Uri, item);\n  }\n\n  filter(options?: Props): Promise<PageList<Json>> {\n    return this.postSearch(this.route().expand(options ?? {}) as Uri);\n  }\n\n  update(item: Json): Promise<Json> {\n    return this.patch(this.routeId().id(item.id) as Uri, item);\n  }\n\n  upsert(item: Json): Promise<Json> {\n    return this.put(this.routeId().id(item.id) as Uri, item);\n  }\n\n  remove(id: Id): Promise<boolean> {\n    return this.delete(this.routeId().id(id) as Uri);\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAaO,IAAM,eAAN,cA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