UNPKG

4.83 kBJavaScriptView Raw
1'use strict';
2
3var Resource = require('./resource');
4var OAuth = require('./oauth').OAuth;
5var helpers = require('./helpers');
6var config = require('../config');
7var util = require('util');
8var capitalize = require('./helpers').capitalize;
9var _ = require('lodash');
10var api;
11
12function configureSchemaFromEnv(schema) {
13 var port;
14
15 if (process.env[ '_7D_API_CLIENT_HOST']) {
16 schema.host = process.env['_7D_API_CLIENT_HOST'];
17 }
18
19 if (process.env[ '_7D_API_CLIENT_SSL_HOST']) {
20 schema.sslHost = process.env['_7D_API_CLIENT_SSL_HOST'];
21 }
22
23 if (process.env['_7D_API_CLIENT_PORT']) {
24 port = Number(process.env['_7D_API_CLIENT_PORT']);
25 if (!isNaN(port)) {
26 schema.port = port;
27 }
28 }
29
30 if (process.env['_7D_API_CLIENT_PREFIX']) {
31 if (process.env['_7D_API_CLIENT_PREFIX'].toLowerCase() === 'empty') {
32 schema.prefix = '';
33 } else {
34 schema.prefix = process.env['_7D_API_CLIENT_PREFIX'];
35 }
36 }
37}
38
39// API
40//
41// Creates a new API wrapper from a schema definition
42//
43// - @param {Object} options - The API options, see below
44// - @param {Object} schema - The definition of the api resources and actions
45// see the assets/7digital-api-schema json file.
46//
47// - @constructor
48// - @param {Object} options
49//
50// The options parameter should have the following properties:
51//
52// - `consumerkey` your application's oauth consumer key
53// - `consumersecret` your application's oauth consumer secret
54// - `format` the response format
55// - `logger` a logger instance for output
56function Api(options, schema) {
57 var prop, resourceOptions, resourceConstructor;
58 var apiRoot = this;
59
60 // Set default options for any unsupplied overrides
61 _.defaults(options, config);
62 this.options = options;
63 this.schema = schema;
64
65 configureSchemaFromEnv(this.schema);
66
67 // Creates a constructor with the pre-built resource as its prototype
68 // this is syntactic sugar to allow callers to new up the resources.
69 function createResourceConstructor(resourcePrototype) {
70 function APIResource(resourceOptions) {
71 // Allow creating resources without `new` keyword
72 if (!(this instanceof APIResource)) {
73 return new APIResource(resourceOptions);
74 }
75
76 resourceOptions = resourceOptions || {};
77 // Override any default options for all requests on this resource
78 _.defaults(resourceOptions.defaultParams,
79 apiRoot.options.defaultParams);
80 _.defaults(resourceOptions.headers,
81 apiRoot.options.headers);
82 _.defaults(resourceOptions, apiRoot.options);
83
84 this.format = resourceOptions.format;
85 this.logger = resourceOptions.logger;
86 this.defaultParams = resourceOptions.defaultParams;
87 this.headers = resourceOptions.headers;
88 }
89
90 APIResource.prototype = resourcePrototype;
91 return APIResource;
92 }
93
94 for (prop in schema.resources) {
95 if (schema.resources.hasOwnProperty(prop)) {
96 resourceOptions = options;
97 resourceOptions.api = this;
98 resourceOptions.resourceDefinition =
99 schema.resources[prop];
100
101 this[prop] = createResourceConstructor(
102 new Resource(resourceOptions, schema));
103 }
104 }
105
106 this['OAuth'] = createResourceConstructor(new OAuth(options));
107}
108
109// Creates a new client with additional / replacement options.
110//
111// - @param {Object} newOptions - the overriden or additional options for the
112// new client
113// - @return {Api} - a new api instance with the merged options
114Api.prototype.reconfigure = function reconfigure(newOptions) {
115 var mergedDefaultParams = _.extend({}, this.options.defaultParams,
116 newOptions.defaultParams);
117 var mergedHeaders = _.extend({}, this.options.headers,
118 newOptions.headers);
119 var mergedOptions = _.extend({}, this.options, newOptions);
120
121 mergedOptions.defaultParams = mergedDefaultParams;
122 mergedOptions.headers = mergedHeaders;
123
124 return new Api(mergedOptions, this.schema);
125};
126
127// Factory method for creating an API wrapper from a JSON schema definition.
128//
129// - @param {Object} options - The API options, see below
130// - @param {Object} schema - The definition of the api resources and actions
131// see the assets/7digital-api-schema json file.
132//
133// The `options` argument can contain the following properties:
134//
135// - *consumerkey* - The OAuth consumer key for your application
136// - *consumersecret* - The OAuth consumer secret for your application
137// - *format* - The format of responses you would like to receive
138// - *logger* - An instance of a logger for output from the wrapper
139Api.build = function build(options, schema) {
140 return new Api(options, schema);
141};
142
143// Factory method for creating an API wrapper using defaults.
144// We need a separate method with an inlined schemapath so that
145// browserify can do the right thing for non-node environments.
146Api.buildDefault = function buildDefault(options) {
147 options = options || config;
148 var schema = require('../assets/7digital-api-schema.json');
149 return new Api(options, schema);
150};
151
152module.exports.Api = Api;