All Projects β†’ unsplash β†’ Unsplash Js

unsplash / Unsplash Js

Licence: mit
πŸ€– A server-side JavaScript wrapper for the Unsplash API

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Unsplash Js

Unsplash rb
πŸ’Ž Ruby wrapper for the Unsplash API.
Stars: ✭ 202 (-87.74%)
Mutual labels:  free, unsplash, images, photos, photography
Exiftool Vendored.js
Fast, cross-platform Node.js access to ExifTool
Stars: ✭ 200 (-87.86%)
Mutual labels:  images, photos, photography
Splash Cli
A simple, command line tool to download Unsplash wallpapers. It’s not intended to be anything particularly fancy β€” it just works.
Stars: ✭ 432 (-73.77%)
Mutual labels:  unsplash, photos, photography
go-unsplash
Go Client for the Unsplash API
Stars: ✭ 70 (-95.75%)
Mutual labels:  photos, photography, unsplash
Datasets
🎁 3,000,000+ Unsplash images made available for research and machine learning
Stars: ✭ 1,805 (+9.59%)
Mutual labels:  unsplash, images, photos
Unsplash Php
πŸ‘» Official PHP wrapper for the Unsplash API
Stars: ✭ 332 (-79.84%)
Mutual labels:  unsplash, images, photos
Moul
The minimalist publishing tool for photographers
Stars: ✭ 113 (-93.14%)
Mutual labels:  photos, photography
Thumbsup
Generate static HTML photo / video galleries
Stars: ✭ 493 (-70.07%)
Mutual labels:  photos, photography
React Grid Gallery
Justified image gallery component for React
Stars: ✭ 571 (-65.33%)
Mutual labels:  images, photos
Photo Affix
πŸ“· Stitch your photos together vertically or horizontally easily!
Stars: ✭ 969 (-41.17%)
Mutual labels:  images, photos
Imagestore
Open source google photos alternative!
Stars: ✭ 429 (-73.95%)
Mutual labels:  images, photos
Opensource Socialnetwork
Open Source Social Network (OSSN) is a social networking software written in PHP. It allows you to make a social networking website and helps your members build social relationships, with people who share similar professional or personal interests. It is available in 16 international languages.
Stars: ✭ 710 (-56.89%)
Mutual labels:  free, photos
Papr
🌁 An Unsplash app for iOS
Stars: ✭ 1,025 (-37.77%)
Mutual labels:  photos, photography
Nanogallery2
a modern photo / video gallery and lightbox [JS library]
Stars: ✭ 488 (-70.37%)
Mutual labels:  images, photos
Bubblepictures
Bubble Pictures for iOS done in Swift
Stars: ✭ 434 (-73.65%)
Mutual labels:  images, photos
Filmulator Gui
Filmulator --- Simplified raw editing with the power of film
Stars: ✭ 522 (-68.31%)
Mutual labels:  photos, photography
Elodie
An EXIF-based photo assistant, organizer, manager and workflow automation tool.
Stars: ✭ 840 (-49%)
Mutual labels:  photos, photography
Androidunplash
An unofficial Unsplash API library for Android
Stars: ✭ 80 (-95.14%)
Mutual labels:  unsplash, photos
Photobrowser.forms
Full screen image viewer(Xamarin.Forms) that includes "pinch to zoom" and "swipe to dismiss" gestures.
Stars: ✭ 75 (-95.45%)
Mutual labels:  images, photos
Hexo Album
add album&photos with Hexo blog
Stars: ✭ 85 (-94.84%)
Mutual labels:  photos, photography

Unsplash

npm

Official Javascript wrapper for the Unsplash API.

Key Links:

Documentation

Installation

$ npm i --save unsplash-js

# OR

$ yarn add unsplash-js

Dependencies

Fetch

This library depends on fetch to make requests to the Unsplash API. For environments that don't support fetch, you'll need to provide polyfills of your choosing. Here are the ones we recommend:

Adding polyfills

createApi receives an optional fetch parameter. When it is not provided, we rely on the globally scoped fetch.

This means that you can set the polyfills in the global scope:

// server
import fetch from 'node-fetch';
global.fetch = fetch;

// browser
import 'whatwg-fetch';

or explicitly provide them as an argument:

import { createApi } from 'unsplash-js';
import nodeFetch from 'node-fetch';

const unsplash = createApi({
  accessKey: 'MY_ACCESS_KEY',
  fetch: nodeFetch,
});

Note: we recommend using a version of node-fetch higher than 2.4.0 to benefit from Brotli compression.

node-fetch and global types

This library presumes that the following types exist in the global namespace:

  • fetch
  • RequestInit
  • Response

By default TypeScript defines these via the "dom" type definitions.

However, if you're targeting Node and you're using node-fetch you should omit the "dom" type definitions using the lib compiler option and then define the required global types manually like so:

import { createApi } from 'unsplash-js';
import * as nodeFetch from 'node-fetch'

declare global {
  var fetch: typeof nodeFetch.default;
  type RequestInit = nodeFetch.RequestInit;
  type Response = nodeFetch.Response;
}
global.fetch = nodeFetch.default;

const unsplash = createApi({
  accessKey: 'MY_ACCESS_KEY',
  fetch: nodeFetch.default,
});

Unfortunately this won't work with node-fetch v3 due to an issue in node-fetch, whereby the global namespace is polluted with the "dom" type definitions: https://github.com/node-fetch/node-fetch/issues/1285.

As a workaround you use a type assertion:

import { createApi } from 'unsplash-js';
import * as nodeFetch from 'node-fetch'

const unsplash = createApi({
  accessKey: 'MY_ACCESS_KEY',
  fetch: nodeFetch.default as unknown as typeof fetch,
});

URL

This library also depends on the WHATWG URL interface:

Note: Make sure to polyfill this interface if targetting older environments that do not implement it (i.e. Internet Explorer or Node < v8).

Note 2: For Node, the URL interface exists under require('url').URL since v8 but was only added to the global scope as of v10.0.0. If you are using a version between v8.0.0 and v10.0.0, you need to add the class to the global scope before using unsplash-js:

URL = require('url').URL;

Usage

Creating an instance

To create an instance, simply provide an Object with your accessKey.

NOTE: If you're using unsplash-js publicly in the browser, you'll need to proxy your requests through your server to sign the requests with the Access Key to abide by the API Guideline to keep keys confidential. We provide an apiUrl property that lets you do so. You should only need to provide one of those two values in any given scenario.

import { createApi } from 'unsplash-js';

// on your node server
const serverApi = createApi({
  accessKey: 'MY_ACCESS_KEY',
  //...other fetch options
});

// in the browser
const browserApi = createApi({
  apiUrl: 'https://mywebsite.com/unsplash-proxy',
  //...other fetch options
});

Making a request

Arguments

All methods have 2 arguments: the first one includes all of the specific parameters for that particular endpoint, while the second allows you to pass down any additional options that you want to provide to fetch. On top of that, the createApi constructor can receive fetch options to be added to every request:

const unsplash = createApi({
  accessKey: 'MY_ACCESS_KEY',
  // `fetch` options to be sent with every request
  headers: { 'X-Custom-Header': 'foo' },
});

unsplash.photos.get(
  { photoId: '123' },
  // `fetch` options to be sent only with _this_ request
  { headers: { 'X-Custom-Header-2': 'bar' } },
);

Example: if you would like to implement request abortion, you can do so like this:

const unsplash = createApi({
  accessKey: 'MY_ACCESS_KEY',
});

const controller = new AbortController();
const signal = controller.signal;

unsplash.photos.get({ photoId: '123' }, { signal }).catch(err => {
  if (err.name === 'AbortError') {
    console.log('Fetch aborted');
  }
});

controller.abort();

Response

When making a request using this SDK, there are 2 possible outcomes to a request.

  • Error: we return a result.errors object containing an array of strings (each one representing one error) and result.source describing the origin of the error (e.g. api, decoding). Typically, you will only have on item in this array.
  • Success: we return a result.response object containing the data.
    • If the request is for a page from a feed, then result.response.results will contain the JSON received from API, and result.response.total will contain the X-total header value indicating the total number of items in the feed (not just the page you asked for).
    • If the request is something other than a feed, then result.response will contain the JSON received from API

You can inspect which one you have by reading the result.type value or checking the contents of result.errors/result.success

const unsplash = createApi({ accessKey: 'MY_ACCESS_KEY' });

// non-feed example
unsplash.photos.get({ photoId: 'foo' }).then(result => {
  if (result.errors) {
    // handle error here
    console.log('error occurred: ', result.errors[0]);
  } else {
    // handle success here
    const photo = result.response;
    console.log(photo);
  }
});

// feed example
unsplash.users.getPhotos({ username: 'foo' }).then(result => {
  if (result.errors) {
    // handle error here
    console.log('error occurred: ', result.errors[0]);
  } else {
    const feed = result.response;

    // extract total and results array from response
    const { total, results } = feed;

    // handle success here
    console.log(`received ${results.length} photos out of ${total}`);
    console.log('first photo: ', results[0]);
  }
});

NOTE: you can also pattern-match on result.type whose value will be error or success:

unsplash.photos.get({ photoId: 'foo' }).then(result => {
  switch (result.type) {
    case 'error':
      console.log('error occurred: ', result.errors[0]);
    case 'success':
      const photo = result.response;
      console.log(photo);
  }
});

Types

The types for this library target TypeScript v3.7 and above.

This library is written in TypeScript. This means that even if you are writing plain JavaScript, you can still get useful and accurate type information. We highly recommend that you setup your environment (using an IDE such as VSCode) to fully benefit from this information:

Function arguments

Response Types

Instance Methods

NOTE: All of the method arguments described here are in the first parameter. See the arguments section for more information.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].