ghostbar/transloadit-api

View on GitHub
docs/templates.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>

<html>
<head>
  <title>Module: Templates</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          
          <h1 id="module-templates">Module: Templates</h1>

          
        

        
      </div>

      
        
        
        
          <div class='highlight'><pre><span class="hljs-pi">'use strict'</span>;</pre></div>
        
      
        
        <p><a name='constructor'></a></p>
<h2 id="constructor">Constructor</h2>

        
      
        
        
        
          <div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Templates</span> <span class="hljs-params">(opts, uri)</span> </span>{
  [<span class="hljs-string">'key'</span>, <span class="hljs-string">'secret'</span>].forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(item)</span> </span>{
    <span class="hljs-keyword">if</span> (opts[item] == <span class="hljs-literal">null</span>)
      <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'There\'s a required argument missing: '</span> + item);
  });

  <span class="hljs-keyword">this</span>.opts = opts;
  <span class="hljs-keyword">this</span>.uri = uri;
}</pre></div>
        
      
        
        <p><a name='create'></a></p>
<h2 id="create-a-template">Create a template</h2>

        
      
        
        <h4 id="arguments">Arguments</h4>
<ul>
<li><code>params</code> — Required<ul>
<li><code>template</code></li>
<li><code>auth</code> is required as well but this is created together with the signature
before doing the <code>POST</code> request inside this method.</li>
</ul>
</li>
<li><code>callback</code> - Function Callback</li>
</ul>
<h4 id="returns">Returns</h4>
<p>A <code>Callback</code> function with <code>(err, body)</code> from the request.</p>
<h4 id="code">Code</h4>

        
          <div class='highlight'><pre>Templates.prototype.create = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(params, callback)</span> </span>{
  <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
  <span class="hljs-keyword">var</span> signature = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./signature.js'</span>))(self.opts);
  <span class="hljs-keyword">var</span> request = <span class="hljs-built_in">require</span>(<span class="hljs-string">'request'</span>);

  <span class="hljs-keyword">if</span> (params == <span class="hljs-literal">null</span>)
    <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'params are required'</span>));

  [<span class="hljs-string">'template'</span>].forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(item)</span> </span>{
    <span class="hljs-keyword">if</span> (params[item] == <span class="hljs-literal">null</span>)
      <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Required param mising: '</span> + item));
  });

  <span class="hljs-keyword">var</span> r = request.post(
    self.uri.base + self.uri.templates,
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(err, response, body)</span> </span>{
      <span class="hljs-keyword">if</span> (err)
        <span class="hljs-keyword">return</span> callback(err);

      <span class="hljs-keyword">return</span> callback(<span class="hljs-literal">null</span>, body);
  });

  <span class="hljs-keyword">var</span> form = r.form();

  <span class="hljs-keyword">var</span> x = signature.create(params);

  form.append(<span class="hljs-string">'signature'</span>, x.signature);
  form.append(<span class="hljs-string">'params'</span>, <span class="hljs-built_in">JSON</span>.stringify(x.params));
};</pre></div>
        
      
        
        <p><a name='retrieve'></a></p>
<h2 id="retrieve-a-template">Retrieve a template</h2>

        
      
        
        <h4 id="arguments">Arguments</h4>
<ul>
<li><code>templateId</code></li>
<li><code>callback</code> — Callback function</li>
</ul>
<h4 id="returns">Returns</h4>
<p>Returns a callback with <code>(err, body)</code>, <code>body</code> being the template JSON content
for the given template ID.</p>
<h4 id="code">Code</h4>

        
          <div class='highlight'><pre>Templates.prototype.retrieve = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(templateId, callback)</span> </span>{
  <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
  <span class="hljs-keyword">var</span> signature = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./signature.js'</span>))(self.opts);
  <span class="hljs-keyword">var</span> request = <span class="hljs-built_in">require</span>(<span class="hljs-string">'request'</span>);

  <span class="hljs-keyword">if</span> (templateId == <span class="hljs-literal">null</span>)
    <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'templateId is required'</span>));

  <span class="hljs-keyword">var</span> x = signature.create();

  request({
    method: <span class="hljs-string">'GET'</span>,
    uri: self.uri.base + self.uri.templates + <span class="hljs-string">'/'</span> + templateId,
    qs: {
      signature: x.signature
    }
  }, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(err, response, body)</span> </span>{
    <span class="hljs-keyword">if</span> (err)
      <span class="hljs-keyword">return</span> callback(err);

    <span class="hljs-keyword">return</span> callback(<span class="hljs-literal">null</span>, body);
  });
};</pre></div>
        
      
        
        <p><a name='edit'></a></p>
<h2 id="edit-a-template">Edit a template</h2>

        
      
        
        <h4 id="arguments">Arguments</h4>
<ul>
<li><code>params</code> — Required<ul>
<li><code>name</code></li>
<li><code>template</code></li>
<li><code>auth</code> is required as well but this is created together with the signature
before doing the <code>PUT</code> request inside this method.</li>
</ul>
</li>
<li><code>templateId</code> — Required</li>
<li><code>callback</code> - Function Callback</li>
</ul>
<h4 id="returns">Returns</h4>
<p>A <code>Callback</code> function with <code>(err, body)</code> from the request</p>
<h4 id="code">Code</h4>

        
          <div class='highlight'><pre>Templates.prototype.edit = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(params, templateId, callback)</span> </span>{
  <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
  <span class="hljs-keyword">var</span> signature = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./signature.js'</span>))(self.opts);
  <span class="hljs-keyword">var</span> request = <span class="hljs-built_in">require</span>(<span class="hljs-string">'request'</span>);

  <span class="hljs-keyword">if</span> (params == <span class="hljs-literal">null</span> || templateId == <span class="hljs-literal">null</span>)
    <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'params and templateId are required'</span>));

  [<span class="hljs-string">'name'</span>, <span class="hljs-string">'template'</span>].forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(item)</span> </span>{
    <span class="hljs-keyword">if</span> (params[item] == <span class="hljs-literal">null</span>)
      <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Required param missing: '</span> + item));
  });

  <span class="hljs-keyword">var</span> x = signature.create(params);
  <span class="hljs-keyword">var</span> payload = {
    params: x.params,
    signature: x.signature
  };

  request({
    method: <span class="hljs-string">'PUT'</span>,
    uri: self.uri.base + self.uri.templates + <span class="hljs-string">'/'</span> + templateId,
    body: <span class="hljs-built_in">JSON</span>.stringify(payload)
  }, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(err, response, body)</span> </span>{
    <span class="hljs-keyword">if</span> (err)
      <span class="hljs-keyword">return</span> callback(err);

    <span class="hljs-keyword">return</span> callback(<span class="hljs-literal">null</span>, body);
  });
};</pre></div>
        
      
        
        <p><a name='remove'></a></p>
<h2 id="remove-a-template">Remove a template</h2>

        
      
        
        <h4 id="arguments">Arguments</h4>
<ul>
<li><code>templateId</code> - required</li>
<li><code>callback</code> - Callback function</li>
</ul>
<h4 id="returns">Returns</h4>
<p>Returns a <code>(err, response)</code> with the response from the request directly.</p>
<h4 id="code">Code</h4>

        
          <div class='highlight'><pre>Templates.prototype.remove = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(templateId, callback)</span> </span>{
  <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
  <span class="hljs-keyword">var</span> signature = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./signature.js'</span>))(self.opts);
  <span class="hljs-keyword">var</span> request = <span class="hljs-built_in">require</span>(<span class="hljs-string">'request'</span>);

  <span class="hljs-keyword">if</span> (templateId == <span class="hljs-literal">null</span>)
    <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'template id is required'</span>));

  <span class="hljs-keyword">var</span> x = signature.create();
  <span class="hljs-keyword">var</span> payload = {
    signature: x.signature
  };

  request({
    method: <span class="hljs-string">'DELETE'</span>,
    uri: self.uri.base + self.uri.templates + <span class="hljs-string">'/'</span> + templateId,
    body: <span class="hljs-built_in">JSON</span>.stringify(payload)
  }, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(err, response, body)</span> </span>{
    <span class="hljs-keyword">if</span> (err)
      <span class="hljs-keyword">return</span> callback(err);

    <span class="hljs-keyword">return</span> callback(<span class="hljs-literal">null</span>, body);
  });
};</pre></div>
        
      
        
        <p><a name='retrieveList'></a></p>
<h2 id="retrieve-a-list-of-templates">Retrieve a list of templates</h2>

        
      
        
        <h4 id="parameters">Parameters</h4>
<ul>
<li><code>params</code> - required. The accepted params are <a href="https://transloadit.com/docs/api-docs#retrieve-template-list">here</a>.</li>
<li><code>callback</code> - Callback function</li>
</ul>
<h4 id="returns">Returns</h4>
<p>Returns a <code>(err, response)</code> with the direct response from the request.</p>
<h4 id="code">Code</h4>

        
          <div class='highlight'><pre>Templates.prototype.retrieveList = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(params, callback)</span> </span>{
  <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
  <span class="hljs-keyword">var</span> signature = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./signature.js'</span>))(self.opts);
  <span class="hljs-keyword">var</span> request = <span class="hljs-built_in">require</span>(<span class="hljs-string">'request'</span>);

  <span class="hljs-keyword">if</span> (params == <span class="hljs-literal">null</span>)
    <span class="hljs-keyword">return</span> callback(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'params are required'</span>));

  <span class="hljs-keyword">var</span> x = signature.create(params);

  request({
    method: <span class="hljs-string">'GET'</span>,
    uri: self.uri.base + self.uri.templates,
    qs: {
      params: params,
      signature: x.signature
    }
  }, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(err, response, body)</span> </span>{
    <span class="hljs-keyword">if</span> (err)
      <span class="hljs-keyword">return</span> callback(err);

    <span class="hljs-keyword">return</span> callback(<span class="hljs-literal">null</span>, body);
  });
};

<span class="hljs-built_in">module</span>.exports = Templates;</pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>