@ovh-api/common
Version:
common class used to enable Ovh Api new calls Syntax
148 lines (147 loc) • 4.48 kB
JavaScript
/**
* common Getter part fot handlers
* - $()
* - $getv/$put()/$post()/$delete()/$cache()
* - path navigation
*/
const commonGet = (key, target) => {
if (key.startsWith('$')) {
// give parameter in path
if (key === '$') {
return (id) => {
// escape '/' char
const idStr = String(id).replace(/\//g, '%2F');
const child = new OvhProxyApi(target._ovhEngine, `${target._path}/${idStr}`, `${target._model}/*`);
return new Proxy(child, handlerChild);
};
}
// $get $post $delete $put
const fnc = (params) => {
const mtd = key.substring(1);
if (mtd === 'cache') {
return target._ovhEngine.cache(target._model, params);
}
else {
// get post put delete
return target._ovhEngine.doRequest(mtd, target._path, target._model, params);
}
};
return fnc.bind(target._ovhEngine);
}
if (key.startsWith('_'))
key = key.substring(1);
const child = new OvhProxyApi(target._ovhEngine, `${target._path}/${key}`, `${target._model}/${key}`);
return new Proxy(child, handlerChild);
};
/**
* handler for all proxy level except the root one
* handle:
* - Object Field
* - $()
* - $getv/$put()/$post()/$delete()
* - path navigation
*/
const handlerChild = {
construct(target, argArray, newTarget) {
return target;
},
get(target, p, receiver) {
if (typeof p === 'symbol')
return target[p];
const key = p.toString();
switch (key) {
case 'toString':
case 'valueOf':
case 'toLocaleString':
return target[p];
}
return commonGet(key, target);
}
};
/**
* handler for the first level of the proxy
* handle:
* - Object Field
* - EventEmitter Field
* - flat get/put/post/delete calls
* - $()
* - $get()/$put()/$post()/$delete()
* - path navigation
*/
const handlerRoot = {
construct(target, argArray, newTarget) {
console.log(argArray);
console.log(newTarget);
return target;
},
get(target, p, receiver) {
if (typeof p === 'symbol')
return target[p];
let key = p.toString();
switch (key) {
// object
case 'toString':
case 'valueOf':
case 'toLocaleString':
// hasOwnProperty
// isPrototypeOf
// propertyIsEnumerable
// constructor
return target[p];
// EventEmitter
case 'addListener':
case 'on':
case 'once':
case 'prependListener':
case 'prependOnceListener':
case 'removeListener':
case 'off':
case 'removeAllListeners':
case 'setMaxListeners':
case 'getMaxListeners':
case 'listeners':
case 'rawListeners':
case 'emit':
case 'eventNames':
case 'listenerCount':
return target[p];
// return (target as unknown as EventEmitter)[p as 'addListener' | 'on' | 'once' | 'prependListener' | 'prependOnceListener' | 'removeListener' | 'off' | 'removeAllListeners' | 'setMaxListeners' | 'getMaxListeners' | 'listeners' | 'rawListeners' | 'emit' | 'eventNames' | 'listenerCount'];
// legacy method only in root level
// @deprecated
case 'get':
case 'put':
case 'post':
case 'delete':
// root level call do not have path parameter so pathTemplate == path
return (path) => (params) => target._ovhEngine.doRequest(key, path, path, params);
}
return commonGet(key, target);
}
};
/**
* for Ovh API 2.0 Proxy
* Data cloned on each Proxy node call
* maintains full PATH for OVH calls
*/
class OvhProxyApi {
_ovhEngine;
_path;
_model;
constructor(ovhEngine, path, model) {
this._ovhEngine = ovhEngine;
this._path = path;
this._model = model || path;
}
toString() {
return 'current path:' + this._path;
}
}
/**
* Build Ovh API 2.0 Proxy
*
* @param ovhEngine
* @param path
*/
export function buildOvhProxy(ovhEngine, path) {
return new Proxy(new OvhProxyApi(ovhEngine, path), handlerRoot);
}