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

ng2-cdf

v2.2.7

Published

Angular2 Content Delivery Framework (CDF)

Downloads

1,824

Readme

ng2-cdf

Angular2 Content Delivery Framework (CDF)

The purpose of cdf-data-island component is to surface JSON data to the caller.

FEATURES:

  • Supports caching of results using ng2-cache (https://github.com/Jackson88/ng2-cache)
  • Supports ability to combine multiple GETs and POSTs URLS into a single Observable block
  • Supports ability to combine requests from multiple domains into a single Observable block

TODOS:

  • test multiple domains
  • need ability to surface errors to caller of cdf-data-island
  • ability to supoort PUTs
  • ability to support DELETEs
  • placing cdf-data-island in GitHub so it can be used in multiple projects

CdfRequestModel:

CdfRequestModel is the class used for gathering the GETs and POSTs http requests that will be called as a single Observable block

export class CdfRequestModel
{
	GetList: string[]; 
	PostList: CdfPostModel[];

	constructor()
	{
	}
}
  • GetList is an array of GET requests
  • PostList is an array of POST models

CdfPostModel:

CdfPostModel is the class describing the data elements needed for a proper POST

export class CdfPostModel
{
	URL: string;
	Body: Object;

	constructor(url: string, body: Object)
	{
		this.URL = url;
		this.Body = body;
	}
}
  • URL is the POST URL request
  • Body is the body of the POST request

IMPLEMENTATION EXAMPLES:

	import { Component, OnInit } 				from '@angular/core';
	import { CdfPostModel, CdfRequestModel } 	from '../../../cdf-data-island/index';	

	@Component({
		selector: 'some-cool-custom-tag',
		templateUrl: './some-cool-custom-tag.component.html',
		styleUrls: [ './some-cool-custom-tag.component.less' ],
		providers: []
	})
	export class SomeCoolCustomComponent implements OnInit
	{
		RequestModel = new CdfRequestModel();

		ngOnInit() 
		{		
			//LIST OF GETS FOR DATA ISLAND		
			this.RequestModel.GetList =
				[
					'http://your-domain.com/something/something/1',
					'http://your-domain.com/something/something/2',
					'http://your-domain.com/something/something/3',
					'http://your-domain.com/something/something/4',
					'http://your-domain.com/something/something/5'
				];


			//POST FOR DATA ISLAND
			this.RequestModel.PostList = 
			[
				new CdfPostModel
				(
					//POST URL:
					'http://your-domain.com/something/something/5?limit=3&metadata=false&full=true',
				
					//POST BODY:
					{
						"_type": "stm:media-production-show",
						"mediaProduction.id": 34
					}
				)
			];
		}			

		/*
		rawJson will be an array where each item in array is the results of a GET or POST in RequestModel
		*/
		onContentReceived(rawJson: any) 
		{
			console.log('CONTENT RECEIVED:', rawJson);
		};	
	}
	/* some-cool-custom-tag.component.html: */
	<cdf-data-island [RequestData]="RequestModel" 
				(onContentReceived)="onContentReceived($event)"
				(onContentError)="onContentError($event)"></cdf-data-island>

	<section>
		/* DISPLAY HTML HERE CONSUMING JSON RESULTS FROM CDF-DATA-ISLAND */
	</section>

CDF-DATA-ISLAND ATTRIBUTES:

  • RequestData is RequestModel data type (see below) containing GETs and POSTs that are called as a single Observable block
  • (onContentReceived) is the event binding that is called when results are returned for each call in Observable block
  • (onContentError) is the event binding that is called when an error happens

MULTIPLE HTTP REQUESTS:

CDF supports the ability to group multiple requests into a single Observable block. Observables are part of new Angular2 RXJS implementation

  • http://www.metaltoad.com/blog/angular-2-http-observables
  • http://blog.thoughtram.io/angular/2016/01/06/taking-advantage-of-observables-in-angular2.html
  • https://scotch.io/tutorials/angular-2-http-requests-with-observables

CDF combines multiple requests using ForkJoin

  • http://www.syntaxsuccess.com/viewarticle/combining-multiple-rxjs-streams-in-angular-2.0
  • http://restlet.com/blog/2016/04/12/interacting-efficiently-with-a-restful-service-with-angular2-and-rxjs-part-2/

The order that HTTP requests are added to the ForkJoin is the exact same order the results are returned.

CDF allows you to add HTTP requests in multiple ways:

  • GetList is an array of GET requests
  • PostList is an array of POST models

CDF adds requests to the ForkJoin observable in the following oreder:

  • GetList
  • PostList

Again, the results returned to your return event handler (onContentReceived) are in the same order the requests are added to the ForkJoin (see above).


MULTIPLE DOMAINS:

CDF supports multiple domains in combining requests into a single Observable block

CDF sees content as the results of making HTTP requests. It does not care where you get your data for your app. CDF cares about REST.

You can combine requests to different domains in a given CDF Observable block.

Fork Join Observable is successful if ALL requests are successful. If one of the requests receives a 401 unauthorized response, CDF will attempt to recreate a valid token 3 times before quiting.

So, in the event a call fails, CDF will take the domain from the URL that failed and lookup the corresponding credentials and attempt to re-establish a valid authentication token.

If a successful token can be established, then CDF will attept to resubmit each URL in the request.

CDF will attempt the resubmission three times before giving up.


CONFIGURING CDF

cdf-settings.service (/src/cdf-data-island/services/cdf-settings.service.ts) is how you configure ng2-cdf. It's constructor accepts an array of CdfConfigModel (one for each domain that will be supported).

CdfConfigModel contains the following data elements. When a request is made to a REST service, CDF uses the domain of the REST request and looks through the CdfConfigModel array based on Domain name to retrieve the credentials necessary to maintain a connection.

	export class CdfConfigModel
	{
		Domain: string;
		EncodedCredentials: string;
		OAuthURL: string;
		Body: string
	}

cdf-settings.service's constructor accepts an array of CdfConfigModel. The calling application must provide the configuration for cdf-settings.service when the calling app is loaded. Here's an example:

   	//ENVIRONMENTAL SETTINGS IN ANGULAR2 (environment.ts)
   	export const environment =
   	{
   		production: false,
   		cachePrefix: 'my-site-dev',	
   		name: 'My Site - Development',
   		version: '2.2.0',
   		Domain_Credentials:
   		[
   			{
   				"domain": "api.cloudcms.com",
   				"encodedCredentials": "XXXXXXXXXXXXXXXXXXXXX",
   				"oAuthURL": "https://api.cloudcms.com/oauth/token/oauth/token",
   				"body": "grant_type=password&scope=api&username=XXXXXXXXXXXXXXXXXXXXX&password=XXXXXXXXXXXXXXXXXXXXX"
   			},
   			{
   				"domain": "api.twitter.com",
   				"encodedCredentials": "XXXXXXXXXXXXXXXXXXXXX",
   				"oAuthURL": "https://api.twitter.com/oauth2/token",
   				"body": "grant_type=client_credentials"
   			}				
   		]	
   	};	 


   	-------------------------------  config.service.ts:

   	import
   	{
   		CdfConfigModel
   	} 								from 'ng2-cdf/lib';

   	@Injectable()
   	export class ConfigService 
   	{		
   		/*
   		CREATE PROPER CONFIGURATION FOR CONTENT DELIVERY FRAMEWORK (CDF)
   		BASED ON REGISTERED DOMAIN'S IN ENVIRONMENT'S Domain_Credentials NODE
   		*/
   		public static GetDomainCredentials() : CdfConfigModel[]
   		{ 
   			let configArray: CdfConfigModel[] = [];

   			//FIND CONFIG IN LIST WITH SAME DOMAIN NAME
   			for (let entry of environment.Domain_Credentials) 
   			{
   				let domainRootUrl = (entry.domain) ? entry.domain : undefined;

   				if (domainRootUrl)
   				{ 
   					let domainModel = new CdfConfigModel();
   					domainModel.Domain = domainRootUrl;
   					domainModel.EncodedCredentials = (entry.encodedCredentials) ? entry.encodedCredentials : undefined;
   					domainModel.OAuthURL = (entry.oAuthURL) ? entry.oAuthURL : undefined;
   					domainModel.Body = (entry.body) ? entry.body : undefined;

   					configArray.push(domainModel);
   				}	
   			}

   			//console.log('configArray:', configArray);
   			
   			return configArray;
   		}
   	}

   	-------------------------------  shared.module.ts:

   	//3RD PARTY...
   	import { CdfModule } 				from 'ng2-cdf/lib';
   	
   	//BUILD DOMAIN CONFIGURATION ARRAY NEEDED FOR CDF MODULE...
   	let configArray = ConfigService.GetDomainCredentials();

   	@NgModule({
   		imports:
   		[
   			CommonModule,
   			RouterModule,

   			//CONFIGURE CONTENT DELIVERY FRAMEWORK (CDF) WITH CREDENTIALS FOR DIFFERENT REST SOURCES		
   			CdfModule.forRoot(configArray)
   		],
   		declarations:
   		[
   			//COMPONENTS
   		],
   		exports:
   		[		
   			//COMPONENTS

   			//MODULES

   			//APPLICATION MODULES

   			//3RD PARTY...
   			CdfModule		
   		]
   	})
   	export class SharedModule
   	{
   		static forRoot(): ModuleWithProviders
   		{
   			return {
   				ngModule: SharedModule,
   				providers:
   				[
   					.... 
   				]
   			};
   		}
   	}