dropbox-v2-api
Advanced tools
Comparing version
@@ -10,3 +10,10 @@ "use strict"; | ||
const promises_1 = require("fs/promises"); | ||
const apiJSON = JSON.parse(await (0, promises_1.readFile)(new URL('./api.json', import.meta.url))); | ||
let apiJSON; | ||
try { | ||
apiJSON = JSON.parse(await (0, promises_1.readFile)(new URL('./api.json', global.import.meta.url))); | ||
} | ||
catch (err) { | ||
apiJSON = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, 'api.json')).toString()); | ||
} | ||
console.log(apiJSON); | ||
const RPC_RESOURCE_CATEGORY = 'rpc'; | ||
@@ -13,0 +20,0 @@ const UPLOAD_RESOURCE_CATEGORY = 'upload'; |
@@ -5,3 +5,10 @@ import request from 'request'; | ||
import { readFile } from 'fs/promises'; | ||
const apiJSON = JSON.parse(await readFile(new URL('./api.json', import.meta.url))); | ||
let apiJSON; | ||
try { | ||
apiJSON = JSON.parse(await readFile(new URL('./api.json', global.import.meta.url))); | ||
} | ||
catch (err) { | ||
apiJSON = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, 'api.json')).toString()); | ||
} | ||
console.log(apiJSON); | ||
const RPC_RESOURCE_CATEGORY = 'rpc'; | ||
@@ -8,0 +15,0 @@ const UPLOAD_RESOURCE_CATEGORY = 'upload'; |
@@ -1,5 +0,4 @@ | ||
import generate from './src/generate-api-description.js' | ||
(async () => { | ||
await generate(); | ||
})() | ||
// require('./src/generate-api-description.js')(); | ||
const {decompress} = require("compress-json"); | ||
const apiJSON = require('./src/api.json'); | ||
console.log(decompress(apiJSON)); |
@@ -1,3 +0,1 @@ | ||
import api from './src/dropbox-api.js'; | ||
export default api; | ||
module.exports = require('./src/dropbox-api.js'); |
{ | ||
"name": "dropbox-v2-api", | ||
"version": "2.5.10-next.2", | ||
"version": "2.5.10-next.3", | ||
"description": "NodeJS Dropbox v2 API wrapper", | ||
"main": "dist/cjs/dropbox-api.js", | ||
"module": "dist/mjs/dropbox-api.js", | ||
"exports": { | ||
".": { | ||
"import": "./dist/mjs/dropbox-api.js", | ||
"require": "./dist/cjs/dropbox-api.js" | ||
} | ||
}, | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"postinstall": "npm run build", | ||
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./fixup", | ||
"generate": "node generate.js", | ||
@@ -35,3 +25,2 @@ "test": "mocha test/**/*.js" | ||
"compress-json": "2.1.2", | ||
"node-fetch": "3.3.0", | ||
"request": "2.88.2" | ||
@@ -53,5 +42,4 @@ }, | ||
"stream-tester": "0.0.5", | ||
"typescript": "4.9.4", | ||
"underscore": "^1.13.1" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # dropbox-v2-api | ||
[api.json]: <https://github.com/adasq/dropbox-v2-api/blob/master/src/api.json> | ||
[api.json]: <https://github.com/adasq/dropbox-v2-api/blob/master/dist/api.json> | ||
## Why this package? | ||
@@ -13,0 +13,0 @@ |
@@ -1,5 +0,6 @@ | ||
import dropboxV2Api from './dropbox-api.js'; | ||
let TEST_LIB_NAME; | ||
export default dropboxV2Api; | ||
TEST_LIB_NAME = './dropbox-api.js'; | ||
// TEST_LIB_NAME = 'dropbox-v2-api'; | ||
module.exports = require(TEST_LIB_NAME); |
@@ -1,10 +0,5 @@ | ||
import request from 'request'; | ||
import stream from 'stream'; | ||
import {decompress} from "compress-json"; | ||
import { readFile } from 'fs/promises'; | ||
const apiJSON = JSON.parse( | ||
await readFile( | ||
new URL('./api.json', import.meta.url) | ||
) | ||
); | ||
const request = require('request'); | ||
const stream = require('stream'); | ||
const {decompress} = require("compress-json"); | ||
const apiJSON = require('./api.json'); | ||
@@ -155,3 +150,3 @@ const RPC_RESOURCE_CATEGORY = 'rpc'; | ||
export default { | ||
module.exports = { | ||
authenticate: function (config) { | ||
@@ -158,0 +153,0 @@ const resourceHandlingFunctions = generateResourcesHandlingFunctions(resourcesDescriptionList, config); |
@@ -1,8 +0,32 @@ | ||
import request from 'request'; | ||
import fs from 'fs'; | ||
import compressor from 'compress-json'; | ||
var cheerio = require('cheerio'); | ||
var request = require('request'); | ||
var fs = require('fs'); | ||
var async = require('async'); | ||
var _ = require('underscore'); | ||
const compressor = require('compress-json'); | ||
//------------------------------------------- | ||
export default generateApiDescription; | ||
async function generateApiDescription(cb) { | ||
var $, | ||
HEADER_AUTH = '--header \"Authorization: Bearer', | ||
HEADER_CT_OCTET_STREAM = '--header \"Content-Type: application/octet-stream', | ||
ENDPOINT_RPC = 'api.dropboxapi.com', | ||
ENDPOINT_CONTENT = 'content.dropboxapi.com', | ||
utils = { | ||
contains: function (text, sub) { | ||
return (text.indexOf(sub) > -1); | ||
}, | ||
hasClass: function (className, classText) { | ||
return classText.trim().split(' ').indexOf(className) > -1; | ||
}, | ||
getTextNode: function (arr) { | ||
var node = _.filter(arr, function (item) { | ||
return item.type === 'text'; | ||
}); | ||
return _.pluck(node, 'data').join(' ').trim(); | ||
} | ||
}; | ||
module.exports = generateApiDescription2 | ||
async function generateApiDescription2(cb) { | ||
const NAMESPACES = ['account', 'auth', 'check', 'contacts', 'file_properties', 'file_requests', 'files', 'sharing', 'users'] | ||
@@ -29,7 +53,10 @@ let error = null; | ||
if (cb) { | ||
cb(error, apiExamples); | ||
} else { | ||
// fs.writeFileSync('./dist/api.json', JSON.stringify(apiExamples, null, '\t')); | ||
fs.writeFileSync('./src/api.json', JSON.stringify(compressor.compress(apiExamples))); | ||
} | ||
} | ||
@@ -58,2 +85,219 @@ | ||
}) | ||
} | ||
} | ||
//------------------------------------------- | ||
function getAPIDescriptionElems() { | ||
var resp = $('.documentation__routes'); | ||
var namespaces = _.map(resp.children(), function (sectionChild) { | ||
var namespace = { | ||
name: sectionChild.attribs.id.replace(/\s/, ''), | ||
}; | ||
var methodDescWrapElems = _.map(sectionChild.children, function (child, i) { | ||
if (child.attribs && child.attribs.class && utils.hasClass('toc-el', child.attribs.class)) { | ||
return child; | ||
} | ||
}); | ||
namespace.el = _.compact(methodDescWrapElems); | ||
return namespace; | ||
}); | ||
return namespaces; | ||
} | ||
function getTextByElem(el) { | ||
el.find('br').replaceWith('\n'); | ||
const links = el.find('a'); | ||
let text = el.text(); | ||
if (links.length > 0) { | ||
_.each(links, (link, i) => { | ||
link = links.eq(i); | ||
const linkText = link.text(); | ||
const linkHref = link.attr('href').replace(/-/g, ''); | ||
if ('#' + linkText === linkHref || linkHref[0] !== '#') return; | ||
link.text(`[${linkText}](${linkHref}-see-docs)`); | ||
}); | ||
} | ||
text = el.text(); | ||
return text.replace(/\n/g, ' ').trim(); | ||
} | ||
function getTextByElem2(el) { | ||
return el.text().trim().replace(/\\s+/g, ' '); | ||
} | ||
function getExampleData(el) { | ||
return el.find('pre').text(); | ||
} | ||
function getReturns(el) { | ||
const parametersExample = el.find('.literal-block').eq(0).text(); | ||
let parametersExampleObject = null; | ||
if (parametersExample.length > 0) { | ||
parametersExampleObject = JSON.parse(parametersExample); | ||
} | ||
return parametersExampleObject; | ||
} | ||
function getParameterList(el) { | ||
const parametersExample = el.find('.literal-block').eq(0).text(); | ||
let parametersExampleObject = null; | ||
if (parametersExample.length > 0) { | ||
parametersExampleObject = JSON.parse(parametersExample); | ||
} | ||
return { | ||
list: getParameterListInner(el), | ||
example: parametersExampleObject | ||
}; | ||
function getParameterListInner(el) { | ||
return _.map(el.find('.field'), function (item) { | ||
var desc = utils.getTextNode(item.children); | ||
item = $(item); | ||
var nestedWrap = item.find('.nested-child'); | ||
if (!!nestedWrap.length) { | ||
const name = item.find('b code').eq(0).text(); | ||
return { | ||
name, | ||
type: item.find('.type').eq(0).text(), | ||
desc: desc, | ||
parameters: _.flatten(_.map(nestedWrap, function (item) { | ||
return getParameterList($(item)) | ||
})) | ||
}; | ||
} else { | ||
const name = item.find('b code').text(); | ||
return { | ||
name, | ||
type: item.find('.type').text(), | ||
desc: desc | ||
}; | ||
} | ||
}); | ||
} | ||
} | ||
function parseMethodElement(wrap) { | ||
var parsers = { | ||
'Description': getTextByElem, | ||
'URL Structure': getTextByElem2, | ||
'Parameters': getParameterList, | ||
'Returns': getReturns, | ||
'Endpoint format': function (elem) { | ||
const endpointCategory = elem.text().trim().toLowerCase(); | ||
const categories = { | ||
'rpc': 'RPC', | ||
'content-upload': 'UPLOAD', | ||
'content-download': 'DOWNLOAD' | ||
}; | ||
return categories[endpointCategory] || null; | ||
}, | ||
'Example': getExampleData | ||
}; | ||
var h3 = wrap.find('h3'); | ||
var dl = wrap.find('dl'); | ||
var dts = $(dl).find('dt'); | ||
var apiMethod = { | ||
name: h3.text() | ||
}; | ||
_.each(dts, function (dt, i) { | ||
var name = $(dts[i]).text(); | ||
var valueEl = $(dt.nextSibling.nextSibling); | ||
if (parsers[name]) { | ||
var value = parsers[name](valueEl); | ||
apiMethod[name] = value | ||
} else { | ||
// console.log('no parser for', name); | ||
} | ||
}); | ||
return apiMethod; | ||
} | ||
function generateApiDescription(cb) { | ||
request('https://www.dropbox.com/developers/documentation/http/documentation', function (err, resp, body) { | ||
if (err) { | ||
console.log('could not retrive documentaion page...'); | ||
return cb ? cb(err) : err; | ||
} | ||
parseBody(body); | ||
}); | ||
function parseBody(body) { | ||
$ = cheerio.load(body); | ||
var fullApi = _.map(getAPIDescriptionElems(), function (section) { | ||
return { | ||
name: section.name, | ||
methods: _.map(section.el, function (el) { | ||
var methodDescription = parseMethodElement($(el)); | ||
return methodDescription; | ||
}) | ||
}; | ||
}); | ||
const fullApiObject = parseApiDescription(fullApi); | ||
const fullApiContent = JSON.stringify(fullApiObject, null, '\t'); | ||
const apiObject = createApiObject(fullApiObject); | ||
const apiContent = JSON.stringify(apiObject, null, '\t'); | ||
if (cb) { | ||
cb(null, fullApiContent); | ||
} else { | ||
fs.writeFileSync('./dist/api.json', apiContent); | ||
fs.writeFileSync('./dist/api-examples.json', fullApiContent); | ||
} | ||
console.log('api description has been generated...'); | ||
} | ||
} | ||
function createApiObject(fullApiObject) { | ||
Object.keys(fullApiObject).forEach((name => { | ||
const apiDescription = fullApiObject[name]; | ||
delete apiDescription.description; | ||
delete apiDescription.returnParameters; | ||
delete apiDescription.parameters.example; | ||
apiDescription.parameters.available = apiDescription.parameters.list.length > 0; | ||
delete apiDescription.parameters.list; | ||
})); | ||
return fullApiObject; | ||
} | ||
function parseApiDescription(apiDescription) { | ||
var parsedApiDescription = {}; | ||
_.each(apiDescription, function (namespace) { | ||
var namespaceName = namespace.name; | ||
_.each(namespace.methods, function (method) { | ||
var methodName = method.name.substr(1); | ||
var resourceName = [namespaceName, methodName].join('/'); | ||
var methodUri = method['URL Structure']; | ||
var methodExample = method['Example'] || null; | ||
var methodParameters = method['Parameters'] || []; | ||
var returnParameters = method['Returns'] || null; | ||
var endpointFormat = method['Endpoint format'] || null; | ||
var description = method['Description'] || null; | ||
var requiresAuthHeader = methodExample === null ? true : utils.contains(methodExample, HEADER_AUTH); | ||
var requiresReadableStream = methodExample === null ? false : utils.contains(methodExample, HEADER_CT_OCTET_STREAM); | ||
parsedApiDescription[resourceName] = { | ||
uri: methodUri, | ||
requiresAuthHeader: requiresAuthHeader, | ||
requiresReadableStream: requiresReadableStream, | ||
category: endpointFormat, | ||
description: description, | ||
parameters: methodParameters, | ||
returnParameters: returnParameters | ||
}; | ||
}); | ||
}); | ||
return parsedApiDescription; | ||
} |
@@ -1,13 +0,7 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import ejs from 'ejs'; | ||
import beautify from 'js-beautify'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const ejs = require('ejs'); | ||
const beautify = require('js-beautify').js_beautify; | ||
import { fileURLToPath } from 'url'; | ||
import {decompress} from "compress-json"; | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
var parsedApiDescription = decompress(JSON.parse(fs.readFileSync(path.join(__dirname, './api.json')))); | ||
var parsedApiDescription = JSON.parse(fs.readFileSync(path.join(__dirname, '../dist/api.json'))); | ||
const options = {}; | ||
@@ -37,2 +31,3 @@ | ||
function prepareExampleByApiDescription(apiDescription, apiName){ | ||
@@ -75,3 +70,3 @@ | ||
const template = ejs.compile(templates[apiDescription.format], options); | ||
const code = beautify.js_beautify(template(apiDescription), { indent_size: 4 }).replace(/\"/g, '\''); | ||
const code = beautify(template(apiDescription), { indent_size: 4 }).replace(/\"/g, '\''); | ||
@@ -78,0 +73,0 @@ return { |
@@ -1,2 +0,2 @@ | ||
import tester from 'stream-tester'; | ||
const tester = require('stream-tester'); | ||
@@ -13,4 +13,4 @@ /** | ||
export default { | ||
module.exports = { | ||
createMockedReadStream | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
4516766
425.49%2
-33.33%14
-6.67%51
4.08%96646
3311.44%0
-100%15
36.36%No
NaN- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed