/
url-insert.js
113 lines (97 loc) · 2.87 KB
/
url-insert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
var _ = require('lodash');
var googleapis = require('googleapis');
var urlshortener = googleapis.urlshortener('v1');
module.exports = {
friendlyName: 'URL insert',
description: 'Create a short URL from given long version',
cacheable: true,
sync: false,
idempotent: false,
inputs: {
longUrl: {
example: 'https://www.google.com',
description: ''
},
analitics: {
typeclass: 'dictionary',
description: ''
},
id: {
example: 'someUniqId',
description: 'id is the short URL that expands to the long URL you provided. '
+ 'If your request includes an auth token, then this URL will be unique. '
+ 'If not, then it might be reused from a previous request to shorten the same URL.',
},
kind: {
example: 'urlshortener#url',
description: 'https://developers.google.com/url-shortener/v1/url/insert'
},
status: {
example: 'SomeStatus',
description: 'https://developers.google.com/url-shortener/v1/url/insert'
},
created: {
example: '1605-11-05T00:00:00.000Z',
description: 'https://developers.google.com/url-shortener/v1/url/insert'
},
fields: {
example: 'analytics,created,id,kind,longUrl,status',
description: 'Selector specifying which fields to include in a partial response'
},
key: {
example: 'AIzaSyAYVDlaoVs_GZw9JNvSclRWH_PEMKII6tc',
description: 'Your api key',
whereToGet: {
url: 'https://developers.google.com/url-shortener/v1/getting_started#APIKey'
}
}
},
exits: {
invalidParameter: {
description: 'Invalid field parameter passed'
},
dailyLimitExceededUnreg: {
description: 'Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.'
},
success: {
variableName: 'result',
description: 'Defaults success hadler'
}
},
fn: function(inputs, exits) {
var params = {
resource: {}
};
var inputParams = _.clone(inputs);
if (inputParams.fields && inputParams.fields.length > 0) {
params.fields = inputParams.fields;
delete inputParams.fields; // Have to remove this
}
// Check and add key
if (inputParams.key && inputParams.key.length > 0) {
params.key = inputParams.key;
delete inputParams.key;
}
_.merge(params.resource, inputParams);
urlshortener.url.insert(params, function(err, response) {
if (err) {
if (!err.code) {
return exits.errro(err);
}
switch(err.code) {
case 400:
return exits.invalidParameter(err);
break;
case 403:
return exits.dailyLimitExceededUnreg(err);
break;
default:
return exits.error(err);
break;
}
}
return exits.success(response);
});
}
};