Create custom traits, responses, resource types, and libraries so that API-led connectivity has a consistent format, response, error type, etc.
Below are steps which will help to generate this custom framework:
- Traits (Headers, Responses)
- Responses (Specified HTTP status like 200, 201, 400, 500, etc.)
- Resource Types (Specific to HTTP methods like GET, POST / GET, PUT and DELETE / POST (with search criteria) and Publish as Custom Library (this can be reused in specific API ex. xAPI, pAPI or sAPI)
- Create specific APIs and reuse the library created in step 3.
1: Create Traits, Headers, Error Type
Create Traits for specific organisation-level headers, error types, etc.
- apiHeaders (Org. level pre-defined headers ex: x-trace-id)
- Error (Error Object/Data Type, which will be defined at Org. level)
Error Code Specific Response Traits
hasBadRequest (400), hasInternalProblem (500), hasNotFound (404), hasRequestItem (Input Request for single Object), hasResponseItem (Output response for single object), hasResponseCollection (Output response for multiple objects/ array of objects).
Similar to these, we have other Traits like hasNotFound.raml (Code: 404), 409, etc., as needed.
2: Create responses against specific HTTP status codes
We do have an example for each specific Error Code:
ex: 400 (ErrorType400.json), similar to this, we can generate 401, 500 related json files for respective error codes.
Also, Error Code has specific patterns (like 3 characters of API name and 5 digits error code — Org. level Error Code Standard.)
3: Custom Library with Resource Types, importing traits and examples of HTTP status codes.
Create a custom library and publish it to Anypoint Exchange as a reusable component.
4: Create API and import library for reusability
Create an example-demo-api, which has all resource types and a couple of endpoints that have specific custom headers, query parameters and responses.
Documentation (Auto Generated)
Conclusion
This will be useful when you have green field implementation or are re-designing your APIs to format standardised consistency, reduce efforts for API governance, quick implementation, and Demo/PoC for API-led connectivity on the MuleSoft Anypoint Platform.