UNPKG

@expressive-analytics/deep-thought-service

Version:

Typescript conversion of Deep Thought Services (formerly providers)

71 lines (66 loc) 2.16 kB
import {HTTPMethod,DTRequest,DTService} from './DTService' import {DTVerifier} from './DTVerifier' export class DTRESTService extends DTService{ protected performAction(action?){ let obj = this.handleREST(action,this.request.method) let str = this.response.objectAsRenderable(obj,this.$public) this.setResponse(str) } protected isMultipleObjects(a){ return Array.isArray(a) } protected handleREST(action?:string,method:HTTPMethod="GET"){ // because OAuth doesn't sign DELETE requests, and doesn't support PATCH, // we use POST... we need to map this back to the appropriate logic // (see mapping in DTOAuth1Delegate) if(action=="actionremove"){ method="DELETE"; action = undefined; } if(action=="actionupsert"){ method="PATCH"; action = undefined; } if(this.actionExists(action)){ // we're already defined, just do it this.recordRequest(action); return this[action](); } this.recordRequest(method); // we're using the method to infer the action // determine whether this is a collection or individual entity const id = this.request.params[this.lookup_column] let is_collection = typeof(this.request.params["id"]) === 'undefined' || this.request.params["id"] == '' switch(method){ case "GET": if(is_collection) return this.actionList(); else return this.actionGet(); case "DELETE": // delete a resource if(is_collection) return this.actionRemoveMany(); else return this.actionRemove(); case "POST": // non-idempotent if(is_collection) return this.actionCreate(); else return this.actionUpdate(); case "PUT": // idempotent, full-resource setter if(is_collection){ this.actionRemoveMany(); return this.actionCreateMany(); }else{ this.actionRemove(); return this.actionCreate(); } case "PATCH": // filter-based upsert if(typeof this.request.params["dt_items"] !== 'undefined') return this.actionUpsertMany(); // patch-many requires a special param else return this.actionUpsert(); default: // e.g. OPTIONS, HEAD return null; } this.response.error(400); } }