<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thomas Werner</title>
    <description>The latest articles on DEV Community by Thomas Werner (@tiguchi).</description>
    <link>https://dev.to/tiguchi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F77832%2F2cb45398-d751-4277-984f-06f0e9e55bb2.jpg</url>
      <title>DEV Community: Thomas Werner</title>
      <link>https://dev.to/tiguchi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tiguchi"/>
    <language>en</language>
    <item>
      <title>Where to Put Response Metadata - Envelope or HTTP Headers?</title>
      <dc:creator>Thomas Werner</dc:creator>
      <pubDate>Sat, 15 Dec 2018 00:28:18 +0000</pubDate>
      <link>https://dev.to/tiguchi/where-to-put-response-metadata---envelope-or-http-headers-4ai9</link>
      <guid>https://dev.to/tiguchi/where-to-put-response-metadata---envelope-or-http-headers-4ai9</guid>
      <description>

&lt;p&gt;Here's some nitpicky head-scratcher I'm overthinking at the moment. Let's assume we have a RESTful API with resource collection endpoints that allow us to search and get paginated results.&lt;/p&gt;

&lt;p&gt;Where to put response metadata such as "next page" URL or next page cursor token?&lt;/p&gt;

&lt;p&gt;There are two ways to do that.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Response Headers
&lt;/h2&gt;

&lt;p&gt;Here's my currently preferred way of adding meta information. It can be added as HTTP headers to the response, while leaving the actual response data untouched:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MyAPI-Next-Page: /users?next=ABCDEFG123
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user-1"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user-2"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Response Envelope
&lt;/h2&gt;

&lt;p&gt;However, many APIs out there (such as Facebook or Twitter) prefer to wrap the response in a data field of an envelope object, and add other meta data to that wrapper:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"pagination"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"next"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/users?next=ABCDEFG123"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user-1"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"user-2"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's even touted as the way to go on the following website that proposes a standard for JSON APIs: &lt;a href="https://jsonapi.org/"&gt;https://jsonapi.org/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I don't like Envelopes
&lt;/h2&gt;

&lt;p&gt;To be honest I started to dislike them fairly recently. A couple of years ago I designed and implemented an ecommerce web API. Inspired by common JSON API design practices out there I actually did wrap search results in envelopes.&lt;/p&gt;

&lt;p&gt;These were the three issues in our case:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The meta data was not really needed by the client apps after all
&lt;/h3&gt;

&lt;p&gt;They use infini-scrollers and simply request the next page until the API responds with an empty result set.&lt;/p&gt;

&lt;p&gt;Also total amount of result items or amount of pages is basically ignored. We didn't need to update the UI with that information, and we did not implement an old-school navigation bar.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Response data is a bit awkward to handle in JavaScript code
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/users'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//                              +-- really?&lt;/span&gt;
    &lt;span class="c1"&gt;//                              v&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;resultList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Sure, this is really just a cosmetic thing that can be easily fixed by renaming the &lt;code&gt;data&lt;/code&gt; property to something else such as &lt;code&gt;results&lt;/code&gt;. But that would also mean breaking compatibility with that &lt;a href="https://jsonapi.org/"&gt;JSON:API standard&lt;/a&gt; I mentioned earlier (just kidding, I don't really care about that standard :-D).&lt;/p&gt;

&lt;h3&gt;
  
  
  3. It repeatedly introduced the same kind of bug in the client apps
&lt;/h3&gt;

&lt;p&gt;The extra jump to the actual result set &lt;code&gt;response.data.data&lt;/code&gt; was often overlooked, which repeatedly caused bugs that were only detected while testing. And some of them remained hidden for a while.&lt;/p&gt;

&lt;p&gt;The added confusion probably also stemmed from the fact that individual resources were not wrapped in an envelope and therefor directly accessible through &lt;code&gt;response.data&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  (EDIT) 4. It's also kinda redundant
&lt;/h3&gt;

&lt;p&gt;As someone over at Stackoverflow pointed out: &lt;a href="https://stackoverflow.com/a/9999335"&gt;HTTP &lt;em&gt;is&lt;/em&gt; already the envelope&lt;/a&gt;. There really shouldn't be a need for wrapping result sets in an envelope data structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why am I still confused?
&lt;/h2&gt;

&lt;p&gt;I assume that very smart people with a ton of hands on practical experience work at Facebook, Twitter and the likes. I also assume they might have run into issues with using HTTP response headers for adding meta data to responses.&lt;/p&gt;

&lt;p&gt;Does anyone know about such issues? I believe it might be problems with proxies that wipe out or alter headers? Or is it JavaScript in browsers that doesn't give full access to all headers? Or are there weird HTTP client libraries out there that don't know how to parse headers?&lt;/p&gt;

&lt;p&gt;What is your experience or opinion?&lt;/p&gt;


</description>
      <category>healthydebate</category>
      <category>rest</category>
      <category>apidesign</category>
      <category>json</category>
    </item>
    <item>
      <title>Trying to update dependencies</title>
      <dc:creator>Thomas Werner</dc:creator>
      <pubDate>Wed, 10 Oct 2018 23:28:12 +0000</pubDate>
      <link>https://dev.to/tiguchi/trying-to-update-dependencies-3hfm</link>
      <guid>https://dev.to/tiguchi/trying-to-update-dependencies-3hfm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fw1y12kssdt6rfem7jpli.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fw1y12kssdt6rfem7jpli.gif" alt="npm"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>npm</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
