js-api-creator

1.0.14 • Public • Published

js-api-creator

How to install

 npm i js-api-creator deletions

How to use

For example server urls : https://myspace.com/space/v1/connectBlogs, https://myspace.com/space/v1/connectBlogs/:id

Check the below, how to convert the server api's as json format.

Note 📓 No need to add this https://myspace.com/space/v1/ prefix for all your api.Just add connectBlogs,connectBlogs/:id.

 
import { constants } from 'js-api-creator'; 
 
let jsonAPIS = {
    "blogs" : { url : "connectBlogs" , method : constants.get } ,
    "blogs.create" : { url : "connectBlogs" , method : constants.post } ,
    "blogs.deleteIt" : { url : "connectBlogs/:id" , method : constants.deleteIt } ,
    "blogs.update" : { url : "connectBlogs/:id" , method : constants.put }  ,
    "blogs.get" : { url : "connectBlogs/:id" , method : constants.get },
    "blogs.getByPermalink" : { url : "connectBlogs/blogByPermalink", method : constants.get } ,
    "blogs.search" : { url : "connectBlogs/search", method : constants.get },
    "blogs.relatedBlogs" : { url : "connectBlogs/relatedArticleSearch" , method : constants.get } ,
    "blogs.like" : { url : "connectBlogs/:id/like", method : constants.post },
    "blogs.disLike" : { url : "connectBlogs/:id/dislike", method : constants.post } ,
    "blogs.attachments" : { url : "connectBlogs/:id/attachments", method : constants.get } ,
    "blogs.attachments.download" : { url : "connectBlogs/:blogId/attachments/:id/content" ,
                                     method : constants.download }  ,
    "blogs.updateFeedback" : { url : "connectBlogs/:id/feedbacks", method : constants.post } ,
    "blogs.comments" : { url : "connectBlogs/:id/comments" , method : constants.get},
    "blogs.comments.get" :{ url : "connectBlogs/:id/comments/:id", method : constants.get },
    "blogs.comments.attachments" :{ url : "connectBlogs/:blogId/comments/:id/attachments", method : constants.get },
    "blogs.comments.attachments.download" : { url : "connectBlogsBlogs/:blogId/comments/:commentId/attachments/:id/content" , 
                                        method : constants.download } ,
    "categories" : { url : "connectCategory" , method : constants.get }
}
 

Now we created a json format of all our api's.

How to handle the multiple id's in same url path.

Example URL : https://myspace.com/space/v1/connectBlogsBlogs/:blogId/comments/:commentId/attachments/:id/content

     let jsonAPIS = {
         "blogs.comments.attachments.download" : { 
        url : "connectBlogsBlogs/:blogId/comments/:commentId/attachments/:attachmentId/content" ,
        method : constants.download
         }
     }
 
     #How to pass the multiple id's to the api : 
 
     blogs.comments.attachments.download({
        blogId : 'efgh-abcd',                    ===> refers blog id
      commentId : 'abcd-efgh',                 ===> refers comment id
      attachmentId : 'xyz-pqrs'                ===> refers attachment id
     })
 
    #URL output will be :
 
    `https://myspace.com/space/v1/connectBlogsBlogs/efgh-abcd/comments/abcd-efgh/attachments/xyz-pqrs/content`

Example - how to create server api's for your app using APIBuilder class.

modueName - used for return the converted api's with this given name.

jsonAPIS - variable json object, we have already created above.

import APIBuilder from 'js-api-creator'; 
let apiBuilder = new APIBuilder( 
       (`moduleName`)=>"https://myspace.com/space/v1/" ,   ===> url prefix will be added for all urls `I have already mentioned`
       (`moduleName`)=>globalParameters,                   ===> global params will be added for all urls
       (`moduleName`)=>headers                             ===> global header will be added for all urls
    );
 
 
let apiModuleObject = apiBuilder.create( `jsonAPIS` , `modueName`);
 
 
#Output : how to call the api.
 
apiModuleObject.`modueName`.blogs( parameters as json , resolve/success , reject/failure ) 
apiModuleObject.`modueName`.blogs.Get( parameters as json , resolve/success , reject/failure  ) 
apiModuleObject.`modueName`.blogs.Search( parameters as json , resolve/success , reject/failure )
apiModuleObject.`modueName`.categories( parameters as json, resolve/success , reject/failure  )
  

If you want override the url for Specifically, when calling the api?.

    apiModuleObject.`modueName`.blogs( { url } , parameters , resolve , reject );

If you want override the header for Specifically, when calling the api?.

    apiModuleObject.`modueName`.blogs( { header } , parameters , resolve , reject );

To change the global url prefix, parameters and header for Specifically.You can use modueName in APIBuilder class.

 new APIBuilder( 
      (`modueName`)=>{
 
         if(`modueName` == "kb"){
           return module-specific-url-prefix as string
         }
         
         return common-url-prefix as string
 
      },
     
      (`modueName`)=>{
         if(`modueName` == "kb"){
           return {module-specific-global-parameters}
         }
         
         return {common-global-parameters} as json
 
      },
 
      (`modueName`)=>{
 
         if(`modueName` == "kb"){
           return {module-specific-headers}
         }
         
         return {common-global-headers} as json
      }
     );

Package Sidebar

Install

npm i js-api-creator

Weekly Downloads

0

Version

1.0.14

License

MIT

Unpacked Size

66.6 kB

Total Files

16

Last publish

Collaborators

  • iniankarthick