@umbraco/headless-client
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -82,7 +82,9 @@ "use strict"; | ||
if (this.endpoint.source === Endpoint_1.EndpointSource.ContentManagement) { | ||
if (this.client.getAPIKey() === null) { | ||
if (!this.client.options.apiKey) { | ||
throw new Error("API Key is missing"); | ||
} | ||
headers["api-key"] = "" + this.client.getAPIKey(); | ||
} | ||
if (this.client.options.apiKey) { | ||
headers['api-key'] = this.client.options.apiKey; | ||
} | ||
options = this.endpoint.options; | ||
@@ -89,0 +91,0 @@ log("options", options); |
@@ -6,2 +6,3 @@ import { ManagementClient, DeliveryClient } from "./Clients"; | ||
language?: string; | ||
apiKey?: string; | ||
} | ||
@@ -13,3 +14,2 @@ /** | ||
readonly options: ClientOptions; | ||
private _apiKey; | ||
constructor(options: ClientOptions); | ||
@@ -31,7 +31,11 @@ /** | ||
* @param apikey API Key | ||
* @deprecated Use `apiKey` on the options instead | ||
*/ | ||
setAPIKey: (apikey: string) => void; | ||
getAPIKey: () => string | null; | ||
/** | ||
* @deprecated Use `apiKey` on the options instead | ||
*/ | ||
getAPIKey: () => string | undefined; | ||
private getEmbeddedData; | ||
private getPagedData; | ||
} |
@@ -59,3 +59,2 @@ "use strict"; | ||
this.options = options; | ||
this._apiKey = null; | ||
/** | ||
@@ -97,7 +96,11 @@ * Get Delivery client for fetching content and media from CDN | ||
* @param apikey API Key | ||
* @deprecated Use `apiKey` on the options instead | ||
*/ | ||
this.setAPIKey = function (apikey) { | ||
_this._apiKey = apikey; | ||
_this.options.apiKey = apikey; | ||
}; | ||
this.getAPIKey = function () { return _this._apiKey; }; | ||
/** | ||
* @deprecated Use `apiKey` on the options instead | ||
*/ | ||
this.getAPIKey = function () { return _this.options.apiKey; }; | ||
this.getEmbeddedData = function (response) { | ||
@@ -104,0 +107,0 @@ if (response.hasOwnProperty('_embedded')) { |
@@ -40,2 +40,24 @@ "use strict"; | ||
var url = 'https://{API_TYPE}.umbraco.io' + endpoint.getPath(); | ||
var params = new URLSearchParams(); | ||
if (endpoint.options) { | ||
if (typeof endpoint.options.pageSize === 'number') { | ||
params.append('pageSize', endpoint.options.pageSize); | ||
} | ||
if (typeof endpoint.options.page === 'number') { | ||
params.append('page', endpoint.options.page); | ||
} | ||
if (typeof endpoint.options.depth === 'number') { | ||
params.append('depth', endpoint.options.depth); | ||
} | ||
if (typeof endpoint.options.hyperlinks === 'boolean') { | ||
params.append('hyperlinks', endpoint.options.hyperlinks); | ||
} | ||
if (typeof endpoint.options.contentType === 'string') { | ||
params.append('contentType', endpoint.options.contentType); | ||
} | ||
} | ||
var queryString = params.toString(); | ||
if (queryString) { | ||
url += "" + (url.indexOf('?') > -1 ? '&' : '?') + queryString; | ||
} | ||
var apiType; | ||
@@ -42,0 +64,0 @@ switch (endpoint.source) { |
export interface CultureOptions { | ||
culture: string; | ||
culture?: string; | ||
} |
export interface DepthOptions { | ||
depth?: string; | ||
depth?: number; | ||
} |
export interface PageOptions { | ||
page?: string; | ||
pageSize?: string; | ||
page?: number; | ||
pageSize?: number; | ||
} |
{ | ||
"name": "@umbraco/headless-client", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Node.js client library for the Umbraco Headless APIs", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -39,11 +39,12 @@ import {Endpoint, EndpointSource} from "./Endpoint"; | ||
if(this.endpoint.source === EndpointSource.ContentManagement) { | ||
if(this.client.getAPIKey() === null) { | ||
if (this.endpoint.source === EndpointSource.ContentManagement) { | ||
if (!this.client.options.apiKey) { | ||
throw new Error("API Key is missing") | ||
} | ||
} | ||
headers["api-key"] = `${this.client.getAPIKey()}` | ||
if (this.client.options.apiKey) { | ||
headers['api-key'] = this.client.options.apiKey | ||
} | ||
const options = this.endpoint.options | ||
@@ -50,0 +51,0 @@ log("options", options) |
@@ -6,4 +6,5 @@ import { ManagementClient, DeliveryClient} from "./Clients"; | ||
export interface ClientOptions { | ||
projectAlias: string | ||
language?: string | ||
projectAlias: string | ||
language?: string | ||
apiKey?: string | ||
} | ||
@@ -17,89 +18,91 @@ | ||
private _apiKey: string|null = null | ||
constructor(public readonly options: ClientOptions) { | ||
constructor(public readonly options: ClientOptions) { | ||
} | ||
} | ||
/** | ||
* Get Delivery client for fetching content and media from CDN | ||
*/ | ||
public readonly delivery = new DeliveryClient(this) | ||
/** | ||
* Get Delivery client for fetching content and media from CDN | ||
*/ | ||
public readonly delivery = new DeliveryClient(this) | ||
/** | ||
* Get Manager Client for managing content on Umbraco headless | ||
*/ | ||
public readonly management = new ManagementClient(this) | ||
/** | ||
* Get Manager Client for managing content on Umbraco headless | ||
*/ | ||
public readonly management = new ManagementClient(this) | ||
/** | ||
* Makes request from and [Endpoint] | ||
*/ | ||
public makeRequest = async <R extends any>(endpoint: Endpoint<R>, data?: any): Promise<R> => { | ||
/** | ||
* Makes request from and [Endpoint] | ||
*/ | ||
public makeRequest = async <R extends any>(endpoint: Endpoint<R>, data?: any): Promise<R> => { | ||
const response = await new ApiRequest<R>(this, endpoint, data).promise() | ||
const items = this.getEmbeddedData(response) | ||
const pageData = this.getPagedData(response) | ||
const response = await new ApiRequest<R>(this, endpoint, data).promise() | ||
const items = this.getEmbeddedData(response) | ||
const pageData = this.getPagedData(response) | ||
if(pageData) { | ||
return { | ||
...pageData, | ||
items | ||
} | ||
} else if (!pageData && items) { | ||
return items | ||
} else { | ||
return response | ||
} | ||
if(pageData) { | ||
return { | ||
...pageData, | ||
items | ||
} | ||
} else if (!pageData && items) { | ||
return items | ||
} else { | ||
return response | ||
} | ||
/** | ||
* Sets the API to be used. | ||
* @param apikey API Key | ||
*/ | ||
public setAPIKey = (apikey: string) => { | ||
this._apiKey = apikey | ||
} | ||
public getAPIKey = () => this._apiKey | ||
} | ||
private getEmbeddedData = (response: any) => { | ||
if(response.hasOwnProperty('_embedded')) { | ||
const keys = Object.keys(response._embedded) | ||
const keyCount = keys.length | ||
if(keyCount === 1) { | ||
const key = keys[0] | ||
return response._embedded[key] | ||
} | ||
} | ||
return null | ||
/** | ||
* Sets the API to be used. | ||
* @param apikey API Key | ||
* @deprecated Use `apiKey` on the options instead | ||
*/ | ||
public setAPIKey = (apikey: string) => { | ||
this.options.apiKey = apikey | ||
} | ||
/** | ||
* @deprecated Use `apiKey` on the options instead | ||
*/ | ||
public getAPIKey = () => this.options.apiKey | ||
private getEmbeddedData = (response: any) => { | ||
if(response.hasOwnProperty('_embedded')) { | ||
const keys = Object.keys(response._embedded) | ||
const keyCount = keys.length | ||
if(keyCount === 1) { | ||
const key = keys[0] | ||
return response._embedded[key] | ||
} | ||
} | ||
private getPagedData = (response: any) => { | ||
const lookForProps = ["_totalItems", "_totalPages", "_page", "_pageSize"] | ||
const keys = Object.keys(response) | ||
return null | ||
} | ||
for(let i=0;i<lookForProps.length;i++) { | ||
const needle = lookForProps[i] | ||
if (keys.indexOf(needle) === -1) return null | ||
} | ||
private getPagedData = (response: any) => { | ||
const lookForProps = ["_totalItems", "_totalPages", "_page", "_pageSize"] | ||
const keys = Object.keys(response) | ||
const object: any = {} | ||
lookForProps.forEach(key => { | ||
object[key.replace(/^_/, '')] = response[key] | ||
}) | ||
for(let i=0;i<lookForProps.length;i++) { | ||
const needle = lookForProps[i] | ||
if (keys.indexOf(needle) === -1) return null | ||
} | ||
return object | ||
const object: any = {} | ||
lookForProps.forEach(key => { | ||
object[key.replace(/^_/, '')] = response[key] | ||
}) | ||
return object | ||
} | ||
} | ||
} |
export enum EndpointSource { | ||
CDN, | ||
Media, | ||
ContentManagement | ||
CDN, | ||
Media, | ||
ContentManagement | ||
} | ||
@@ -15,58 +13,82 @@ | ||
constructor( | ||
public readonly source: EndpointSource, | ||
public readonly path: string, | ||
public readonly urlParams: any, | ||
public readonly method: 'get'|'GET'|'post'|'POST'|'put'|'PUT'|'delete'|'DELETE', | ||
public readonly options?: Options | ||
) { | ||
constructor( | ||
public readonly source: EndpointSource, | ||
public readonly path: string, | ||
public readonly urlParams: any, | ||
public readonly method: 'get'|'GET'|'post'|'POST'|'put'|'PUT'|'delete'|'DELETE', | ||
public readonly options?: Options | ||
) { | ||
} | ||
/** | ||
* Replace path with urlParams | ||
*/ | ||
getPath = () => { | ||
const keys = Object.keys(this.urlParams) | ||
if(keys.length === 0) { | ||
return this.path | ||
} | ||
/** | ||
* Replace path with urlParams | ||
*/ | ||
getPath = () => { | ||
const keys = Object.keys(this.urlParams) | ||
if(keys.length === 0) { | ||
return this.path | ||
} | ||
let path = this.path | ||
keys.forEach(key => { | ||
const value = this.urlParams[key] | ||
let path = this.path | ||
keys.forEach(key => { | ||
const value = this.urlParams[key] | ||
const regEx = new RegExp(`{${key}}`) | ||
path = path.replace(regEx, value) | ||
const regEx = new RegExp(`{${key}}`) | ||
path = path.replace(regEx, value) | ||
}) | ||
}) | ||
return path | ||
} | ||
static getURLAddress = (endpoint: Endpoint) => { | ||
let url = 'https://{API_TYPE}.umbraco.io' + endpoint.getPath() | ||
return path | ||
const params = new URLSearchParams() | ||
if (endpoint.options) { | ||
if (typeof endpoint.options.pageSize === 'number') { | ||
params.append('pageSize', endpoint.options.pageSize) | ||
} | ||
if (typeof endpoint.options.page === 'number') { | ||
params.append('page', endpoint.options.page) | ||
} | ||
if (typeof endpoint.options.depth === 'number') { | ||
params.append('depth', endpoint.options.depth) | ||
} | ||
if (typeof endpoint.options.hyperlinks === 'boolean') { | ||
params.append('hyperlinks', endpoint.options.hyperlinks) | ||
} | ||
if (typeof endpoint.options.contentType === 'string') { | ||
params.append('contentType', endpoint.options.contentType) | ||
} | ||
} | ||
const queryString = params.toString() | ||
static getURLAddress = (endpoint: Endpoint) => { | ||
let url = 'https://{API_TYPE}.umbraco.io' + endpoint.getPath() | ||
if (queryString) { | ||
url += `${url.indexOf('?') > -1 ? '&' : '?'}${queryString}` | ||
} | ||
let apiType: string | ||
switch(endpoint.source) { | ||
case EndpointSource.CDN: | ||
apiType = "cdn" | ||
break | ||
let apiType: string | ||
switch(endpoint.source) { | ||
case EndpointSource.CDN: | ||
apiType = "cdn" | ||
break | ||
case EndpointSource.ContentManagement: | ||
apiType = "api" | ||
break | ||
default: | ||
apiType = "cdn" | ||
break | ||
} | ||
case EndpointSource.ContentManagement: | ||
apiType = "api" | ||
break | ||
default: | ||
apiType = "cdn" | ||
break | ||
} | ||
url = url.replace("{API_TYPE}", apiType) | ||
url = url.replace("{API_TYPE}", apiType) | ||
return url | ||
return url | ||
} | ||
} | ||
} |
export interface CultureOptions { | ||
culture: string | ||
culture?: string | ||
} |
export interface DepthOptions { | ||
depth?: string | ||
depth?: number | ||
} |
export interface PageOptions { | ||
page?: string | ||
pageSize?: string | ||
page?: number | ||
pageSize?: number | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
212877
3267