node-pexels
Advanced tools
Comparing version 0.0.4 to 0.0.5
export { DefaultPexelsClient as Client } from './lib/client/default'; | ||
export * from './lib/client/interfaces'; |
@@ -1,16 +0,20 @@ | ||
import { IPexelsClient, IPexelsImage, IPexelsResponse } from './interfaces'; | ||
import { IPexelsClient, IPexelsImage, IPexelsResponse, TPexelsImageSource, IImageData } from './interfaces'; | ||
export declare class DefaultPexelsClient implements IPexelsClient { | ||
protected apiKey: string; | ||
static endpoint: string; | ||
static BASE_ENDPOINT: string; | ||
static PHOTO_RESOURCE: string; | ||
static SEARCH_RESOURCE: string; | ||
static POPULAR_RESOURCE: string; | ||
constructor(apiKey: string); | ||
protected endpoint: string; | ||
constructor(apiKey: string, https?: boolean); | ||
useHttps(v?: boolean): void; | ||
photo(id: number): Promise<IPexelsImage>; | ||
search(query: string, perPage?: number, page?: number): Promise<IPexelsResponse>; | ||
popular(perPage?: number, page?: number): Promise<IPexelsResponse>; | ||
fetch(photo: IPexelsImage, src: TPexelsImageSource): Promise<IImageData>; | ||
private validatePhotoMethodParams; | ||
private validatePageAndPerPageArguments; | ||
private validateSearchMethodParams; | ||
private makeAbsoluteRequest; | ||
private makeRequest; | ||
} |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var got = require("got"); | ||
var path = require("path"); | ||
var DefaultPexelsClient = (function () { | ||
function DefaultPexelsClient(apiKey) { | ||
function DefaultPexelsClient(apiKey, https) { | ||
if (https === void 0) { https = true; } | ||
this.apiKey = apiKey; | ||
this.endpoint = ''; | ||
this.useHttps(https); | ||
} | ||
DefaultPexelsClient.prototype.useHttps = function (v) { | ||
if (v === void 0) { v = true; } | ||
this.endpoint = (v ? 'https' : 'http') + "://" + DefaultPexelsClient.BASE_ENDPOINT; | ||
}; | ||
DefaultPexelsClient.prototype.photo = function (id) { | ||
@@ -42,2 +61,10 @@ try { | ||
}; | ||
DefaultPexelsClient.prototype.fetch = function (photo, src) { | ||
var url = photo.src[src]; | ||
return this.makeAbsoluteRequest(url, {}, false) | ||
.then(function (data) { return ({ | ||
format: path.extname(url.split('?')[0].split('#')[0]).replace('.', ''), | ||
data: data | ||
}); }); | ||
}; | ||
DefaultPexelsClient.prototype.validatePhotoMethodParams = function (id) { | ||
@@ -66,8 +93,6 @@ if (typeof id !== 'number') { | ||
}; | ||
DefaultPexelsClient.prototype.makeRequest = function (resource, queryStringObject) { | ||
return got(DefaultPexelsClient.endpoint + resource, { | ||
json: true, | ||
query: queryStringObject, | ||
headers: { Authorization: ' ' + this.apiKey } | ||
}) | ||
DefaultPexelsClient.prototype.makeAbsoluteRequest = function (url, queryStringObject, json) { | ||
if (queryStringObject === void 0) { queryStringObject = {}; } | ||
if (json === void 0) { json = true; } | ||
return got.get(url, __assign(__assign(__assign({}, (json ? { json: true } : { encoding: null })), (Object.keys(queryStringObject).length ? { query: queryStringObject } : {})), { headers: { Authorization: this.apiKey } })) | ||
.then(function (response) { return response.body; }) | ||
@@ -78,3 +103,8 @@ .catch(function (error) { | ||
}; | ||
DefaultPexelsClient.endpoint = 'https://api.pexels.com/v1/'; | ||
DefaultPexelsClient.prototype.makeRequest = function (resource, queryStringObject, json) { | ||
if (queryStringObject === void 0) { queryStringObject = {}; } | ||
if (json === void 0) { json = true; } | ||
return this.makeAbsoluteRequest(this.endpoint + resource, queryStringObject, json); | ||
}; | ||
DefaultPexelsClient.BASE_ENDPOINT = 'api.pexels.com/v1/'; | ||
DefaultPexelsClient.PHOTO_RESOURCE = 'photos'; | ||
@@ -81,0 +111,0 @@ DefaultPexelsClient.SEARCH_RESOURCE = 'search'; |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
export declare type TPexelsImageSource = 'original' | 'large' | 'medium' | 'small' | 'portrait' | 'landscape' | 'tiny'; | ||
@@ -20,2 +21,6 @@ export interface IPexelsImage { | ||
} | ||
export interface IImageData { | ||
format: string; | ||
data: Buffer; | ||
} | ||
export interface IPexelsClient { | ||
@@ -25,2 +30,3 @@ photo(id: number): Promise<IPexelsImage>; | ||
popular(perPage?: number, page?: number): Promise<IPexelsResponse>; | ||
fetch(photo: IPexelsImage, src: TPexelsImageSource): Promise<IImageData>; | ||
} |
{ | ||
"name": "node-pexels", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Node client for https://www.pexels.com API", | ||
@@ -31,18 +31,18 @@ "main": "build/index.js", | ||
"devDependencies": { | ||
"@types/chai": "^4.1.7", | ||
"@types/chai-json-schema": "^1.4.3", | ||
"@types/got": "^9.2.2", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.18", | ||
"@types/chai": "^4.2.3", | ||
"@types/chai-json-schema": "^1.4.5", | ||
"@types/got": "^9.6.7", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.7.8", | ||
"chai": "^4.2.0", | ||
"chai-json-schema": "^1.4.0", | ||
"coveralls": "^3.0.2", | ||
"chai-json-schema": "^1.5.1", | ||
"coveralls": "^3.0.6", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.2.0", | ||
"tslint": "^5.1.0", | ||
"typescript": "^3.2.2" | ||
"mocha": "^6.2.1", | ||
"tslint": "^5.20.0", | ||
"typescript": "^3.6.3" | ||
}, | ||
"dependencies": { | ||
"got": "^9.5.0" | ||
"got": "^9.6.0" | ||
} | ||
} |
@@ -18,2 +18,5 @@ # node-pexels | ||
#### v 0.0.5 | ||
Fixes, deps update, etc. | ||
#### v 0.0.4 | ||
@@ -35,2 +38,3 @@ Added retrieving single photo method ([#1](https://github.com/dlukanin/node-pexels/pull/1)) | ||
const Client = require('node-pexels').Client; | ||
const fs = require('fs'); | ||
@@ -43,3 +47,27 @@ const client = new Client('your-api-key'); | ||
console.log(results); | ||
if (results.photos.length > 0) { | ||
const photo = results.photos[0]; | ||
const source = 'medium'; | ||
return client.fetch(photo, source) | ||
} else { | ||
throw new Error('no results found'); | ||
} | ||
}) | ||
.then((file) => { | ||
return new Promise( | ||
(reject, resolve) => { | ||
fs.writeFile( | ||
`./img.${file.format}`, | ||
file.data, | ||
(err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(); | ||
} | ||
); | ||
} | ||
); | ||
}) | ||
.catch((error) => { | ||
@@ -65,5 +93,11 @@ // Something bad happened | ||
### fetch(photo: IPexelsImage, src: TPexelsImageSource): Promise\<IImageData\> | ||
Fetch a photo file from the selected source. | ||
#### IPexelsResponse, IPexelsImage | ||
Responses from pexels api. | ||
#### IImageData | ||
Object containing an image buffer, and format. | ||
You can find schemas [here](https://github.com/dlukanin/node-pexels/blob/master/src/test/response_schema.ts) | ||
@@ -75,4 +109,1 @@ and response example on the [Pexels API](https://www.pexels.com/api) page. | ||
Client use https. You can use http (if you *really* want to) by modifying `endpoint` property of Client function. | ||
#### I want to have Buffer object instead of image url, what should i do? | ||
Current version of module can't do this (hope to implement it in some future releases), you should do it in your own code. |
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
18277
10
170
105
1
Updatedgot@^9.6.0