@angular/common/http#HttpResponse TypeScript Examples

The following examples show how to use @angular/common/http#HttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: fake-backend.service.ts    From ng-conf-2020-workshop with MIT License 6 votes vote down vote up
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        this.users = JSON.parse(this.localStorage.getItem('users')) || [];
        return of(null).pipe(mergeMap(() => {
            // login user
            if (request.url.endsWith('/login') && request.method === 'POST') {
                return this.loginHandle(request);
            }

            // register user
            if (request.url.endsWith('/register') && request.method === 'POST') {
                const user = this.getStorageUser(request);
                return this.registerHandle(user);
            }

            // login user with external provider
            if (request.url.endsWith('/extlogin') && request.method === 'POST') {
                const user = this.getStorageExtUser(request);
                return this.registerHandle(user, true);
            }

            // Microsoft-specific OIDC discovery URI
            if (request.url.endsWith('ms-discovery/keys') && request.method === 'GET') {
                return of(new HttpResponse({ status: 200, body: msKeys }));
            }

            return next.handle(request);
        }))
        .pipe(materialize())
        .pipe(dematerialize());
    }
Example #2
Source File: graphql.structure.ts    From App with MIT License 6 votes vote down vote up
public query<T>(options: GraphQL.QueryOptions & GraphQL.WithVar): Observable<HttpResponse<GraphQL.QueryResult<T>> | null> {
		return this.restService.createRequest<GraphQL.QueryResult<T>>('post', '/gql', {
			body: {
				query: options.query.replace(/\s{2,}/g, ''),
				variables: options.variables
			},
			auth: options.auth,
			runOnSSR: options.runOnSSR
		}, 'v2').pipe(
			RestService.onlyResponse()
		);
	}
Example #3
Source File: fileadmin.service.ts    From CloudeeCMS with Apache License 2.0 6 votes vote down vote up
public upload(files: Set<File>, s3uploadPath: string, s3policy: any, CCMaxAge: string): { [key: string]: Observable<number> } {
        const status = {}; // observables map
        let maxAge = CCMaxAge;
        if (!maxAge || maxAge === '') { maxAge = '259200'; }
        files.forEach(file => {
            const formData: FormData = new FormData();
            formData.append('Content-Type', file.type);
            // tslint:disable-next-line: forin
            for (const f in s3policy.fields) { formData.append(f, s3policy.fields[f]); } // inherit all fields from policy
            formData.append('ACL', 'public-read'); // must appear before file contents!
            formData.append('Cache-Control', 'max-age=' + maxAge);
            const targetFilename = s3uploadPath + file.name;
            console.log('upload', targetFilename);
            formData.append('key', targetFilename);
            formData.append('file', file); // , file.name);

            const req = new HttpRequest('POST', s3policy.url, formData, { reportProgress: true });
            const progress = new Subject<number>();

            // IMPORTANT: exclude POST upload URL from httpinterceptor!!
            this.http.request(req).subscribe(event => {
                if (event.type === HttpEventType.UploadProgress) {
                    const percentDone = Math.round((100 * event.loaded) / event.total);
                    progress.next(percentDone); // update progress bar
                } else if (event instanceof HttpResponse) {
                    progress.complete(); // done uploading this file
                }
            });

            status[file.name] = {
                progress: progress.asObservable(),
                s3key: targetFilename,
                nm: file.name
            };
        });
        return status;
    }
Example #4
Source File: default-headers.interceptor.ts    From WowUp with GNU General Public License v3.0 6 votes vote down vote up
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get the auth token from the service.

    // Clone the request and replace the original headers with
    // cloned headers, updated with the authorization.
    // const cloneReq = req.clone({
    //   headers: req.headers.set("startTimestamp", Date.now().toString()),
    // });
    const start = Date.now();
    const method = req.method;
    const url = req.urlWithParams;

    // send cloned request with header to the next handler.
    return next.handle(req).pipe(
      tap((response: any) => {
        try {
          if (response instanceof HttpResponse) {
            const t = Date.now() - start;
            console.log(`[${method}] ${url} ${response.status} ${t}ms`);
          }
        } catch (e) {
          console.error(e);
        }
      })
    );
  }
Example #5
Source File: authorization.service.ts    From alura_angular_rxjs_1 with MIT License 6 votes vote down vote up
requestToken(user: UserLogin): Observable<HttpResponse<User>> {
    return this.http.post<User>(
      TOKEN_ENDPOINT,
      {
        userName: user.userName,
        password: user.password,
      },
      { observe: 'response' }
    );
  }
Example #6
Source File: attribution.service.ts    From barista with Apache License 2.0 5 votes vote down vote up
public attributionByScanIdIdGet(id: number, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Array<ProjectDistinctLicenseAttributionDto>>>;