{ "version": 3, "sources": ["../ui/app/mn.http.request.js"], "sourcesContent": ["/*\nCopyright 2020-Present Couchbase, Inc.\n\nUse of this software is governed by the Business Source License included in\nthe file licenses/BSL-Couchbase.txt. As of the Change Date specified in that\nfile, in accordance with the Business Source License, use of this software will\nbe governed by the Apache License, Version 2.0, included in the file\nlicenses/APL2.txt.\n*/\n\nimport {Subject, of, merge, NEVER, zip } from 'rxjs';\nimport {HttpErrorResponse } from '@angular/common/http';\nimport {catchError, switchMap, shareReplay, mapTo, filter, map,\n tap} from 'rxjs/operators';\n\nimport {MnHelperService} from './mn.helper.service.js';\n\nexport {MnHttpRequest, MnHttpGroupRequest};\n\nclass MnHttpGroupRequest {\n constructor(httpMap) {\n this.request = new Subject();\n this.httpMap = httpMap;\n this.fakeMap = Object.keys(this.httpMap).reduce((acc, name) => {\n acc[name] = new Subject();\n return acc;\n }, {});\n }\n\n clearError() {\n Object.keys(this.httpMap).forEach((key) => this.httpMap[key].clearError());\n }\n\n addError() {\n this.error =\n zip.apply(null, this.getHttpGroupStreams.bind(this)(\"response\"))\n .pipe(filter((responses) =>\n responses.find((resp) =>\n resp instanceof HttpErrorResponse)))\n\n return this;\n }\n\n addSuccess() {\n this.success =\n zip.apply(null, this.getHttpGroupStreams.bind(this)(\"response\"))\n .pipe(filter((responses) =>\n !responses.find((resp) =>\n resp instanceof HttpErrorResponse)));\n return this;\n }\n\n doOrderedRequest(data) {\n Object.keys(this.httpMap).forEach((key) => {\n if (!data.get(key)) {\n data.set(key, null);\n }\n });\n Array.from(data.keys()).forEach((key) => {\n if (data.get(key) === null) {\n this.fakeMap[key].next(null);\n } else {\n this.httpMap[key].post(data.get(key));\n }\n });\n }\n\n post(data) {\n data = data || {};\n this.request.next();\n if (data instanceof Map) {\n this.doOrderedRequest(data);\n } else {\n Object.keys(this.httpMap).forEach((key) => this.httpMap[key].post(data[key]));\n }\n }\n\n getHttpGroupStreams(stream) {\n return Object.keys(this.httpMap).reduce((result, key) => {\n result.push(merge(this.httpMap[key][stream], this.fakeMap[key]));\n return result;\n }, []);\n }\n\n addLoading() {\n this.loading =\n merge(\n zip.apply(null, this.getHttpGroupStreams.bind(this)(\"response\")).pipe(mapTo(false)),\n this.request.pipe(mapTo(true)));\n return this;\n }\n}\n\nclass MnHttpRequest {\n constructor(call) {\n this.request = new Subject();\n this._errorSubject = new Subject();\n this._loadingSubject = new Subject();\n this.addResponse(call);\n }\n\n clearError() {\n this._errorSubject.next(null);\n }\n\n addResponse(call) {\n let errorsAndSuccess = switchMap((data) => call(data).pipe(catchError((err) => of(err))));\n this.response = this.request.pipe(errorsAndSuccess,\n shareReplay({refCount: true, bufferSize: 1}));\n return this;\n }\n\n addError(modify) {\n let extractErrorsPipe =\n switchMap((rv) => {\n if (rv instanceof HttpErrorResponse) {\n return of(rv);\n } else if (MnHelperService.prototype.isJson(rv) && rv.includes(\"errors\")) {\n return of(new HttpErrorResponse({error: rv}));\n } else {\n return NEVER;\n }\n });\n\n var error = merge(\n this._errorSubject,\n this.response.pipe(\n extractErrorsPipe,\n map(function (rv) {\n if (!!rv.error && MnHelperService.prototype.isJson(rv.error)) {\n let val = JSON.parse(rv.error);\n val.status = rv.status;\n return val;\n } else {\n return rv;\n }\n }),\n (modify ? modify : tap()),\n shareReplay({refCount: true, bufferSize: 1})));\n\n this.error = error;\n\n return this;\n }\n\n addLoading() {\n this.loading = merge(this._loadingSubject, this.response.pipe(mapTo(false)));\n return this;\n }\n\n addSuccess(modify) {\n var success =\n this.response.pipe(\n filter((rv) => !(rv instanceof HttpErrorResponse)),\n shareReplay({refCount: true, bufferSize: 1})\n );\n if (modify) {\n success = success.pipe(modify);\n }\n this.success = success;\n return this;\n }\n\n post(data) {\n this._loadingSubject.next(true);\n this.request.next(data);\n }\n}\n"], "mappings": "+RAmBA,4BAAyB,CACvB,YAAY,QAAS,CACnB,KAAK,QAAU,GAAI,SACnB,KAAK,QAAU,QACf,KAAK,QAAU,OAAO,KAAK,KAAK,SAAS,OAAO,CAAC,IA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