JSPM

  • Created
  • Published
  • Downloads 4077
  • Score
    100M100P100Q134530F
  • License MIT

A library for comfortable use of Permissions API in Angular applications

Package Exports

  • @ng-web-apis/permissions

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ng-web-apis/permissions) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Perissions API for Angular

Part of Web APIs for Angular

npm version npm bundle size Travis (.com) Coveralls github angular-open-source-starter

This is a library to use Permissions API with Angular.

Install

If you do not have @ng-web-apis/common:

npm i @ng-web-apis/common

Now install the package:

npm i @ng-web-apis/permissions

How to use

PermissionsService

Import service in your component:

import { PermissionsService } from '@ng-web-apis/permissions';

...
constructor(private readonly permissions: PermissionsService) {}

Now, use the service to retrieve the state of the permission in question. Below is an example of checking the permission to use geolocation:

const geolocationStatus$ = this.permissions.state('geolocation');
geolocationStatus$.subscribe(geolocationStatus => doSomething(geolocationStatus));

Note, that a call to the permissions.state() returns an observable, which will emit new values in case the state for the permission in question changes. If you need to get state just once and stop observing the permission, you can use take(1) RxJs operator:

geolocationStatus$
    .pipe(take(1))
    .subscribe(geolocationStatus => doSomething(geolocationStatus));

The observable is cold, meaning if there are no active subscriptions, it doesn't track the status of the permission.

Tokens

The library also provides a tokens to simplify working with Permissions API:

  • PERMISSIONS_SUPPORT returns true if user's browser supports Permissions API
export class YourComponent {
  constructor(
    @Inject(PERMISSIONS_SUPPORT) private readonly permissionsSupport: boolean
  ) {}
    ...

See also

Other Web APIs for Angular by @ng-web-apis

Open-source

Do you also want to open-source something, but hate the collateral work? Check out this Angular Open-source Library Starter we’ve created for our projects. It got you covered on continuous integration, pre-commit checks, linting, versioning + changelog, code coverage and all that jazz.