Giter Club home page Giter Club logo

permissions's Introduction

ng-web-apis logo Permissions API for Angular

Part of Web APIs for Angular

npm version npm bundle size CI 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
  ) {}
    ...

Browser support

IE / Edge Firefox Chrome Safari
79+ 46+ 43+

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.

permissions's People

Contributors

dmitryefimenko avatar dtslvr avatar marsibarsi avatar waterplea avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.