npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qipp-services-resource

v1.0.5

Published

Resource service for Angular application.

Downloads

5

Readme

qipp-services-resource Build Status npm version js-standard-style

General

The resource module is composed of one core resource provider, and two top-level providers in order to interact with the API and the auth servers.

Install

npm i qipp-services-resource

Angular usage

resource provider

This provider should not be used directly as it is the underlaying backbone of the apiResource and the authResource. It is based upon the $http method, which is a promise.

Please note that this provider directly takes care of the authorization with the API server, by providing an access token in the headers.

Moreover, the renewal of this access token is made internally, in case of requests giving a 401 (expired or invalid access token). That means that a failing request is made again in that case.

The pubsub service is attached on the resource, allowing the use of event listeners (see apiResource).

apiResource provider

Use this provider to make any API request. Some default parameters must be set in the config phase of your angular application:

// Mandatory property.
apiResourceProvider.defaults.host = 'https://core.qipp.com'
// Prefix and itemsPerPage have both default values.
apiResourceProvider.defaults.prefix = '/'
apiResourceProvider.defaults.itemsPerPage = 10

Note that the itemsPerPage property is used for the page size of the API response, in the case of collections.

Requests

General

The first argument of the apiResource() method must be the API endpoint you are requesting.

You can provide parameters to requests as properties of params.common, a second argument of the apiResource method:

$scope.thingId = 123
$scope.resource = apiResource('things/:id', {
    params: {common: {id: thingId}}
})
Filtering

Some API endpoints accept filtering properties, please refer to the API documentation:

$scope.thingId = 123
$scope.resource = apiResource('things/:id/files', {
    params: {
        common: {
            id: thingId,
            'filter[category]': 'manual'
        }
    }
})

The filtering property could also be an array:

$scope.thingId = 123
$scope.resource = apiResource('things/:id/files', {
    params: {
        common: {
            id: thingId
            'filter[category][0]': 'manual',
            'filter[category][1]': 'technical-data'
        }
    }
})
Disabling the limit

You can get raw collections, overpassing the limit setting this parameter to minus one:

$scope.userId = 123
$scope.resource = apiResource('users/:id/things', {
    params: {
        common: {
            id: userId,
            'limit': -1
        }
    }
})

Responses

Paging

For collections, you get back the all the paging information as the enum property in the resource:

$scope.userId = 123
$scope.resource = apiResource('users/:id/things', {
    params: {common: {id: userId}}
})
$scope.resource
    .$get()
    .then(function () {
        console.log($scope.resource.enum)
        /*
        Enum is an object:
        {
            items: 6, // here we have a total of 6 items for this collection.
            limit: 5, // the limit has been set to 5 items per page.
            page: 1,  // we are on page one.
            pages: 2  // two pages are available.
        }
        */
    })

You can also get it in the direct response from the backend:

$scope.resource
    .$get()
    .then(function (response) {
        console.log(response)
        /*
        The raw response is an object:
        {
            _embedded: {...}, // here are the embedded items of the collection.
            _links: {...},    // see the links section.
            limit: 5,
            page: 1,
            pages: 2,
            total: 6
        }
        */
    })
Links

For every request, the API response includes a links section (could be _links or links, depending if you ar looking at the resource or the response).

Using the last request, you get links as an object:

{
    first: { href: '/api/v1/users/123/things?page=1&limit=5' },
    last: { href: '/api/v1/users/123/things?page=2&limit=5' },
    next: { href: '/api/v1/users/123/things?page=2&limit=5' },
    self: { href: '/api/v1/users/123/things?page=1&limit=5' }
}

You can notice that the links make the API crawable, in this case with a clear focus on the paging of the given collection. But the links could also be a way to get other endpoints:

$scope.userId = 123
$scope.resource = apiResource('users/:id', {
    params: {common: {id: userId}}
})
$scope.resource
    .$get()
    .then(function (response) {
        console.log(response._links)
        /*
        {
            collections: {
                href: '/api/v1/users/123/collections'
            },
            self: {
                href: '/api/v1/users/123'
            },
            things: {
                href: '/api/v1/users/123/things'
            }
        }
        */
    })
Promise callbacks

The apiResource() method is a promise, as the $http one, with .success and .error callbacks:

$scope.thingId = 123
$scope.resource = apiResource('things/:id', {
    params: {common: {id: thingId}}
})
$scope.resource
    .$get()
    .success(function () {
        // Do something.
    })
    .error(function () {
        // Do something else.
    })
CRUD methods

To interact with the API endpoints, the apiResource() could be queried with the following REST methods:

// GET
$scope.resource.$get()
// POST
$scope.resource.$create()
// PATCH
$scope.resource.$update()
// DELETE
$scope.resource.$destroy()
Using the event listeners (with pubsub)

You can attach listeners to you resource, matching the CRUD method names plus the error or success word, for example:

// Create success listener.
$scope.resource
    .$on('createsuccess', callback)
// Delete error listener.
$scope.resource
    .$on('deleteerror', callback)

Please refer to the qipp-services-pubsub documentation for further explanation.

authResource provider

Use this provider to make any auth request, such as login or register. Some default parameters must be set in the config phase of your angular application:

// Mandatory property.
authResourceProvider.defaults.host = 'https://auth.qipp.com'
// Prefix has a default value.
authResourceProvider.defaults.prefix = '/'
// Set the auth client id in the authResource.
authResourceProvider.defaults.clientId = 'YOUR_CLIENT_ID'

Note that the authResource() method is based upon the apiResource() one, which means that the documentation of the latter apply to the former.

Tools

Linting with StandardJS

Please refer to the JavaScript Standard Style for general rules.

npm run lint

Unit testing with Karma

npm test

Requirements

Angular

Qipp modules

Licence

Released under the MIT license by qipp.