Home Reference Source Test Repository
public class | source

Identity

Encapsulates all of the logic for communication with the Identity Service in the NPR One API.

Note that consumers should not be accessing this class directly but should instead use the provided pass-through functions in the main NprOneSDK class.

Example:

How to change a user's station using station search
const nprOneSDK = new NprOneSDK();
nprOneSDK.config = { ... };
nprOneSDK.getUser() // optional; verifies that you have a logged-in user
    .then(() => {
       return nprOneSDK.searchStations('wnyc');
    })
    .then((stations) => {
        const stationId = stations[0].id; // in reality, you'd probably have the user select a station, see the StationFinder for detail
        nprOneSDK.setUserStation(stationId);
    });

Test:

Method Summary

Public Methods
public

Creates a temporary user from the NPR One API and use that user's access token for subsequent API requests.

Caution: most clients are not authorized to use temporary users.

public

followShow(aggregationId: number | string): Promise<User>

Indicates that the user wishes to follow, or subscribe to, the show, program, or podcast with the given numeric ID.

public

Gets user metadata, such as first and last name, programs they have shown an affinity for, and preferred NPR One station.

public

Sets a user's favorite NPR station. Note that this function will first validate whether the station with the given ID actually exists, and will return a promise that rejects if not.

public

unfollowShow(aggregationId: number | string): Promise<User>

Indicates that the user wishes to unfollow, or unsubscribe from, the show, program, or podcast with the given numeric ID.

Public Methods

public createTemporaryUser(): Promise<User> source

Creates a temporary user from the NPR One API and use that user's access token for subsequent API requests.

Caution: most clients are not authorized to use temporary users.

Return:

Promise<User>

Throw:

TypeError

if an OAuth proxy is not configured or no client ID is set

Test:

public followShow(aggregationId: number | string): Promise<User> source

Indicates that the user wishes to follow, or subscribe to, the show, program, or podcast with the given numeric ID. Followed shows will appear more frequently in a user's list of recommendations.

Note that at this time, because we have not yet implemented search in this SDK, there is no way to retrieve a list of aggregation (show) IDs through this SDK. You can either add functionality to your own app that makes an API call to GET https://api.npr.org/listening/v2/search/recommendations with a program name or other search parameters, or wait until we implement search in this SDK (hopefully later this year).

Params:

NameTypeAttributeDescription
aggregationId number | string

The aggregation (show) ID, which is either an integer or a numeric string (e.g. 123 or '123')

Return:

Promise<User>

Throw:

TypeError

if the passed-in aggregation (show) ID is not either a number or a numeric string

Test:

public getUser(): Promise<User> source

Gets user metadata, such as first and last name, programs they have shown an affinity for, and preferred NPR One station.

Return:

Promise<User>

Test:

public setUserStation(stationId: number | string): Promise<User> source

Sets a user's favorite NPR station. Note that this function will first validate whether the station with the given ID actually exists, and will return a promise that rejects if not.

Params:

NameTypeAttributeDescription
stationId number | string

The station's ID, which is either an integer or a numeric string (e.g. 123 or '123')

Return:

Promise<User>

Test:

public unfollowShow(aggregationId: number | string): Promise<User> source

Indicates that the user wishes to unfollow, or unsubscribe from, the show, program, or podcast with the given numeric ID. See followShow for more information.

Params:

NameTypeAttributeDescription
aggregationId number | string

The aggregation (show) ID, which is either an integer or a numeric string (e.g. 123 or '123')

Return:

Promise<User>

Throw:

TypeError

if the passed-in aggregation (show) ID is not either a number or a numeric string

Test: