{"id":10677,"date":"2016-04-12T17:00:47","date_gmt":"2016-04-12T15:00:47","guid":{"rendered":"http:\/\/flaven.fr\/?p=10677"},"modified":"2016-04-12T17:01:51","modified_gmt":"2016-04-12T15:01:51","slug":"swagger-api-node-documenter-une-api-avec-swagger","status":"publish","type":"post","link":"https:\/\/flaven.fr\/2016\/04\/swagger-api-node-documenter-une-api-avec-swagger\/","title":{"rendered":"Swagger, API, Node &#8211; Documenter une API avec Swagger"},"content":{"rendered":"\n<p>Swagger, c&#8217;est un assortiment d&#8217;outils indispensables pour documenter une API. L&#8217;aspect le plus saisissant de cet outil est sa facult\u00e9 \u00e0 g\u00e9n\u00e9rer une documentation digne de ce nom , une documentation qui peut-\u00eatre maintenu et pas seulement par les d\u00e9veloppeurs. <\/p>\n<p>1. Une fois dans la console, l&#8217;installation se fait avec la commande suivante. On fait une installation globale.<\/p>\n<pre lang=\"bash\">npm install -g swagger<\/pre>\n<p>2. Se mettre dans le r\u00e9pertoire o\u00f9 on veut cr\u00e9er son app<br \/>\ncd \/votre_chemin_vers\/doc_api_swagger_io\/<\/p>\n<p>3. Cr\u00e9er votre projet avec swagger. On choisit un nom pour l&#8217;api <code>api_2_movies<\/code> et on seletionne ensuite <code>express<\/code><\/p>\n<pre lang=\"bash\">swagger project create<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/flaven.fr\/wp-content\/uploads\/2016\/04\/using_swagger_io_1.jpg\" width=\"640\" height=\"480\" alt=\"Swagger, API,  - Documenter une API avec Swagger\"><\/p>\n<p>Un fichier nomm\u00e9 <code>swagger.yaml<\/code> va se cr\u00e9er automatiquement. C&#8217;est ce fichier par exemple que vous allez pouvoir \u00e9diter afin de la modifier au fur et \u00e0 mesure que vous concevez votre API avec node.<\/p>\n<p><b>Le r\u00e9sultat est visible \u00e0 cette adresse http:\/\/127.0.0.1:10010\/hello?name=Scott<\/b><\/p>\n<pre lang=\"bash\">swagger project start<\/pre>\n<p><b>Le r\u00e9sultat est visible \u00e0 cette adresse http:\/\/127.0.0.1:50226\/#\/<\/b><\/p>\n<pre lang=\"bash\">swagger project edit<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/flaven.fr\/wp-content\/uploads\/2016\/04\/using_swagger_io_4.jpg\" width=\"640\" height=\"480\" alt=\"Swagger, API,  - Documenter une API avec Swagger\"><\/p>\n<p>D&#8217;autres commandes pouvant \u00eatre utile :<\/p>\n<p><b>V\u00e9rifier si votre fichier <code>swagger.yaml<\/code> est valide.<\/b><\/p>\n<pre lang=\"bash\">swagger project verify<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/flaven.fr\/wp-content\/uploads\/2016\/04\/using_swagger_io_3.jpg\" width=\"640\" height=\"480\" alt=\"Swagger, API,  - Documenter une API avec Swagger\"><\/p>\n<p>Pour sauvegarder la documentation  <\/p>\n<pre lang=\"bash\">npm install swagger-tools --save<\/pre>\n<pre lang=\"bash\">swagger project start<\/pre>\n<p><b>Il est que vous deviez installer toutes les d\u00e9pendances.<\/b><\/p>\n<pre lang=\"bash\">npm install<\/pre>\n<pre lang=\"bash\">npm install swagger-tools --save<\/pre>\n<pre lang=\"bash\">swagger project start<\/pre>\n<p><b>Pour g\u00e9n\u00e9rer la documentation http:\/\/localhost:10010\/docs, il suffit d&#8217;ajouter les lignes marqu\u00e9es par le commentaire <code>\/* For docs *\/<\/code> dans le fichier app.js<\/b><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/flaven.fr\/wp-content\/uploads\/2016\/04\/using_swagger_io_2.jpg\" width=\"640\" height=\"480\" alt=\"Swagger, API,  - Documenter une API avec Swagger\"><\/p>\n<pre lang=\"javascript\">\r\n\t'use strict';\r\n\r\n\tvar SwaggerExpress = require('swagger-express-mw');\r\n\t\/* For docs *\/\r\n\tvar SwaggerUi = require('swagger-tools\/middleware\/swagger-ui');\r\n\tvar app = require('express')();\r\n\tmodule.exports = app; \/\/ for testing\r\n\r\n\tvar config = {\r\n\t  appRoot: __dirname \/\/ required config\r\n\t};\r\n\r\n\tSwaggerExpress.create(config, function(err, swaggerExpress) {\r\n\t  if (err) { throw err; }\r\n\r\n\t  \/* For docs *\/\r\n\t  app.use(SwaggerUi(swaggerExpress.runner.swagger));\r\n  \r\n\t  \/\/ install middleware\r\n\t  swaggerExpress.register(app);\r\n\r\n\t  var port = process.env.PORT || 10010;\r\n\t  app.listen(port);\r\n\r\n\t  if (swaggerExpress.runner.swagger.paths['\/hello']) {\r\n\t    console.log('try this:\\ncurl http:\/\/127.0.0.1:' + port + '\/hello?name=Scott');\r\n\t  }\r\n\t});\r\n\t\r\n<\/pre>\n<p>Source : <a href=\"http:\/\/robferguson.org\/2015\/06\/06\/build-your-microservices-api-with-swagger\/\" target=\"_blank\">http:\/\/robferguson.org\/2015\/06\/06\/build-your-microservices-api-with-swagger\/<\/a><\/p>\n<h4>Un autre exemple d&#8217;API<\/h4>\n<p>Voil\u00e0 un exemple beaucoup plus abouti qui mixe swagger, une API qui se cr\u00e9er au fil de l&#8217;eau avec express, mongoose et lodash et une utilisation de Postman pour manipuler cette API nouvellement cr\u00e9\u00e9e.<br \/>\nSource : <a href=\"https:\/\/www.youtube.com\/watch?v=i9yOksgUQ9Y\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=i9yOksgUQ9Y<\/a><\/p>\n<h2>En savoir plus<\/h2>\n<ul>\n<li>Un validateur de YML<br \/><a href=\"http:\/\/www.yamllint.com\/\" target=\"_blank\">http:\/\/www.yamllint.com\/<\/a><\/li>\n<li>Swagger, The World&#8217;s Most Popular Framework for APIs.<br \/><a href=\"http:\/\/swagger.io\/\" target=\"_blank\">http:\/\/swagger.io\/<\/a><\/li>\n<li>Une d\u00e9monstration convaincante et en live de la r\u00e9daction d&#8217;une documentation<br \/><a href=\"http:\/\/editor.swagger.io\/#\/\" target=\"_blank\">http:\/\/editor.swagger.io\/#\/<\/a><\/li>\n<li>text<br \/><a href=\"https:\/\/github.com\/swagger-api\" target=\"_blank\">https:\/\/github.com\/swagger-api<\/a><\/li>\n<li>Getting started with Swagger and Swaggervel<br \/><a href=\"http:\/\/blog.cjwfuller.com\/2015\/05\/30\/swaggervel\/\" target=\"_blank\">http:\/\/blog.cjwfuller.com\/2015\/05\/30\/swaggervel\/<\/a><\/li>\n<li>Integrate Swagger into Laravel<br \/><a href=\"https:\/\/www.marcoraddatz.com\/en\/2015\/07\/21\/integrate-swagger-into-laravel\/\" target=\"_blank\">https:\/\/www.marcoraddatz.com\/en\/2015\/07\/21\/integrate-swagger-into-laravel\/<\/a><\/li>\n<li>API Blueprint. A powerful high-level API description language for web APIs.<br \/><a href=\"https:\/\/apiblueprint.org\/\" target=\"_blank\">https:\/\/apiblueprint.org\/<\/a><\/li>\n<li>Un tutorial sur Swagger<br \/><a href=\"http:\/\/idratherbewriting.com\/pubapis_swagger\/\" target=\"_blank\">http:\/\/idratherbewriting.com\/pubapis_swagger\/<\/a><\/li>\n<li>Get your swagger on!<br \/><a href=\"http:\/\/blog.catchsoftware.com\/2013\/08\/get-your-swagger-on\/\" target=\"_blank\">http:\/\/blog.catchsoftware.com\/2013\/08\/get-your-swagger-on\/<\/a><\/li>\n<li>Swagger-PHP Documentation<br \/><a href=\"http:\/\/zircote.com\/swagger-php\/\" target=\"_blank\">http:\/\/zircote.com\/swagger-php\/<\/a><\/li>\n<li>Create your RESTful API with Laravel, the PHP Framework<br \/><a href=\"https:\/\/www.udemy.com\/laravel-5-php-framework-agile-and-practical-php-restful-api\/\" target=\"_blank\">https:\/\/www.udemy.com\/laravel-5-php-framework-agile-and-practical-php-restful-api\/<\/a><\/li>\n<li>X Crash Course &#8211; Swagger Integration with Node\/Express (excellent)<br \/><a href=\"https:\/\/www.youtube.com\/watch?v=i9yOksgUQ9Y\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=i9yOksgUQ9Y<\/a><\/li>\n<li>Building APIs with Swagger, Designing and coding APIs in Node.js.<br \/><a href=\"http:\/\/radar.oreilly.com\/2015\/09\/building-apis-with-swagger.html\" target=\"_blank\">http:\/\/radar.oreilly.com\/2015\/09\/building-apis-with-swagger.html<\/a><\/li>\n<li>Swagger for Node.js HTTP API Design<br \/><a href=\"https:\/\/blog.risingstack.com\/swagger-nodejs\/\" target=\"_blank\">https:\/\/blog.risingstack.com\/swagger-nodejs\/<\/a><\/li>\n<li>How to Create a REST API with Node.js and Express<br \/><a href=\"https:\/\/html5hive.org\/how-to-create-rest-api-with-node-js-and-express\/\" target=\"_blank\">https:\/\/html5hive.org\/how-to-create-rest-api-with-node-js-and-express\/<\/a><\/li>\n<li>Generate client stubs &#038; document your REST-API using Swagger &#038; Spring by Johannes Fiala<br \/><a href=\"https:\/\/www.youtube.com\/watch?v=43GhBbP--oI\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=43GhBbP&#8211;oI<\/a><\/li>\n<li>Webcast: How We Built the Online Swagger Editor<br \/><a href=\"https:\/\/www.youtube.com\/watch?v=5xDVe-t6pnQ\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=5xDVe-t6pnQ<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Swagger, c&#8217;est un assortiment d&#8217;outils indispensables pour documenter une API. L&#8217;aspect le plus saisissant de cet outil est sa facult\u00e9 \u00e0 g\u00e9n\u00e9rer une documentation digne&hellip; <\/p>\n<p class=\"text-center\"><a href=\"https:\/\/flaven.fr\/2016\/04\/swagger-api-node-documenter-une-api-avec-swagger\/\" class=\"more-link\">Continue reading &rarr; <span class=\"screen-reader-text\">Swagger, API, Node &#8211; Documenter une API avec Swagger<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10683,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bf_ai_meta_description":"Documenting APIs with Swagger tools ensures maintainable, professional documentation. Follow steps to install, create, and edit Swagger projects for Node APIs.","bf_ai_og_title":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3439,3437,3454,3444,3447,3448,3449,3450,3435,3452],"tags":[2183,571,2322,2323],"class_list":["post-10677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apis-integration","category-business-case-studies","category-journalism-writing","category-programming-databases","category-technology-trends","category-tools-productivity","category-tutorials-how-to","category-ux-product-design","category-web-development","category-wordpress-cms","tag-api","tag-node","tag-postman","tag-swagger"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3Vuhl-2Md","jetpack_featured_media_url":"https:\/\/flaven.fr\/wp-content\/uploads\/2016\/04\/using_swagger_io_b.jpg","_links":{"self":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts\/10677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/comments?post=10677"}],"version-history":[{"count":2,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts\/10677\/revisions"}],"predecessor-version":[{"id":10684,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts\/10677\/revisions\/10684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/media\/10683"}],"wp:attachment":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/media?parent=10677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/categories?post=10677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/tags?post=10677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}