Last active
June 12, 2025 22:35
-
-
Save mcsee/ac3ed6c1ef4cb889369765418ee4b3e4 to your computer and use it in GitHub Desktop.
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// user-api-v1.json - Version 1 (maintained) | |
{ | |
"id": 317, | |
"name": "Mr Nimbus", | |
"email": "[email protected]", | |
"nationalities": "Brazilian,Canadian,Oceanic" | |
} | |
// user-api-v2.json - Version 2 | |
// (new structure, backward compatible) | |
{ | |
"id": 317, | |
"userId": 317, | |
"name": "Mr Nimbus", | |
"fullName": "Mr Nimbus", | |
"email": "[email protected]", | |
"emailAddress": "[email protected]", | |
"createdAt": "2018-12-09T18:30:00Z", | |
"nationalities": "Brazilian,Canadian,Oceanic" | |
"nationalitiesList": ["Brazilian", "Canadian", "Oceanic"] | |
} | |
// user-api-v3.json - Version 3 | |
// (new structure, backward not compatible) | |
{ | |
"userId": 317, | |
"fullName": "Mr Nimbus", | |
"emailAddress": "[email protected]", | |
"createdAt": "2018-12-09T18:30:00Z", | |
"nationalitiesList": ["Brazilian", "Canadian", "Oceanic"] | |
} | |
// client-code-versioned.js | |
const API_VERSION = 'v1'; | |
fetch(`/api/${API_VERSION}/users/317`) | |
.then(response => response.json()) | |
.then(user => { | |
document.getElementById('name').textContent = user.name; | |
document.getElementById('email').textContent = user.email; | |
// V1 handles comma-separated string | |
document.getElementById('nationalities').textContent | |
= user.nationalities; | |
}); | |
// Or with content negotiation | |
fetch('/api/users/317', { | |
headers: { | |
'Accept': 'application/vnd.api+json;version=1' | |
} | |
}) | |
.then(response => response.json()) | |
.then(user => { | |
document.getElementById('name').textContent = user.name; | |
document.getElementById('email').textContent = user.email; | |
document.getElementById('nationalities').textContent | |
= user.nationalities; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment