Sign inSign up

guna3006/tmfm

By guna3006

•Updated about 2 years ago

The TM Forum Open API mocker.

Image
API management
0

139

guna3006/tmfm repository overview

⁠TM Forum Open API

The TM Forum Open API mocker is crafted to enhance your testing and development journey by seamlessly simulating interactions. This tool boosts efficiency and precision in your projects, elevating your overall experience.

⁠Getting Started:

⁠1. Prerequisite
  • Fleet Badge/ VSCode Badge
  • Docker Badge
  • HTTPie Badge / Postman Badge
⁠2. Usage:
  • Execute the Docker Badge with:

    -if Windows Badge use Batch Badge

    -if MacOSX Badge or Linux Badge use Shell Badge

⁠3. Customised Function
  • JS Badge

    • It imports required JS Badge:
      • json-server
      • http
      • https
      • path
      • fs
      • _ as (lodash)
    • It import custom functions from JS Badge.
    • It configures routes using a rewriter if a routes file exists.
    • It loads initial database data from a JS Badge file.
    • It merges additional database files from a custom directory.
    • It sets up a watch on the custom database directory to dynamically update the database when files change.
    • It creates a JS Badge router and sets up middleware for static file serving and request parsing.
    • It integrates custom functionality from JS Badge into the server using the tmf.main function.
    • It starts HTTP and HTTPS servers, listening on ports 9000 and 8000 respectively.
    • It logs server startup information including protocol and port numbers.
  • JS Badge - function is designed to handle incoming HTTP requests within application. Let's break down the functionality of this module:

    • JS Badge - module for setting HTTP headers.

    • JS Badge - module for handling suspicious HTTP headers.

    1. JS Badge

      • JS Badge

        • It extracts some information from the request object (req) to determine the version, API name, and ID.
        • It checks if the query parameter includes fields, limit, or neither.
        • If fields are included, it filters the response by specific fields using a helper function filterByFields.
        • If limit is included, it returns the response as is.
        • If neither fields nor limit is present, it filters the response by query parameters using another helper function filterByQueryParam.
        • It handles cases where the ID is present or absent and returns appropriate responses.
        • If the ID is not found, it returns a 404 error.
        • If the API name is api and ID is list, it returns a list of API names from the database.
        • If none of the conditions are met, it returns a 404 error.

        JS Badge

        • It extracts the first query parameter and its value from the request object (req.query).
        • It handles URL-encoded spaces by replacing "%20" with actual spaces.
        • It converts string values of true and false to boolean values.
        • It checks if both the query parameter object and its value are present, returning a 400 error if not.
        • It filters the database (db) based on the query parameter object and value.
        • If filtered data is found, it returns a 200 status code with the filtered data.
        • If the database is invalid or not an array, it returns a 500 error.
        • If no data matches the query, it returns a 204 status code indicating no content.

        JS Badge

        • It extracts the API version and requested fields from the request object (req).
        • It sets default fields based on the API version.
        • It ensures that the apiName parameter is an array.
        • It checks if requested fields are provided in the query, returning a 400 error if not.
        • It handles cases where apiName is empty or not provided, returning a 404 error.
        • It constructs a map of fields to be filtered based on requested fields or default fields.
        • It iterates over each item in the apiName array and filters the fields based on the constructed map.
        • If filtered data is found, it returns the result; otherwise, it returns a 204 status code indicating no content.
    2. JS Badge

      • JS Badge
        • It imports the fs module for file system operations.
        • It extracts various information from the request object (req), such as protocol, hostname, port, version, and apiName.
        • It constructs the output file path based on the API name. It modifies the request body (reqBody) by adding a unique ID generated by the Faker library and constructing an API endpoint (href).
        • It handles differences in API versions (v4 and v5) by modifying the request body accordingly.
        • It prepares the new data object (newData) to be written to the file.
        • It checks if the output file exists, reads its content if so, appends the new data to it, and writes it back to the file. If the file doesn't exist, it writes the new data directly.
        • It logs the success of data writing or updating.
        • It returns a 201 status code along with the modified request body if successful. If any error occurs during file operations, it logs the error and returns a 404 status code along with the error message.
    3. JS Badge

      • JS Badge
        • It extracts the API name and ID from the request URL.
        • It searches for the entry in the database corresponding to the provided API name and ID.
        • It retrieves the updated data from the request body.
        • If the entry is found in the database:
          • It checks if the updated data differs from the existing data.
          • If the content is the same, it returns a 204 status indicating no content has been modified.
          • It constructs the updated entry by merging the existing entry with the updated data, prioritizing specific fields based on the presence of certain keys.
          • It replaces the existing entry with the updated one in the database.
          • It returns a 201 status along with the updated entry.
        • If the entry is not found in the database, it returns a 404 status along with an error message indicating that the provided ID couldn't be found.
    4. JS Badge

      • JS Badge
        • It extracts the API name and ID from the request URL.
        • It checks if the ID parameter is present and not undefined. If not, it returns a 400 error indicating that the ID parameter is required for deletion.
        • It finds the index of the entry with the provided ID in the database.
        • If the entry is found:
          • It removes the entry from the database using splice.
          • It returns a 204 status along with a success message indicating that the entry has been deleted.
        • If the entry is not found in the database:
          • It returns a 404 error indicating that no entry was found for the given ID.
    5. JS Badge

      • JS Badge
        • It extracts the API name and ID from the request URL.
        • It searches for the entry in the database corresponding to the provided API name and ID.
        • If the entry is not found, it returns a 404 error along with a message indicating that the provided ID couldn't be found in the specified API.
        • It retrieves the updated data from the request body.
        • It checks if the updated data is the same as the existing data for each key. If so, it returns a 204 status indicating no content has been modified. It constructs the updated entry by merging the existing entry with the updated data.
        • It finds the index of the existing entry in the database.
        • If the entry is found:
          • It updates the entry in the database with the new data.
          • It returns a 200 status along with the updated entry.
        • If the entry is not found in the database, it returns a 500 error indicating an internal server error.
    6. JS Badge

      • JS Badge
        • It imports necessary modules Ajv (JSON schema validator), fs (file system), path, and chokidar (file watcher).
        • It extracts the options type and variable to check from the request URL.
        • It defines an enum OptionsTypeEnum to represent different types of options.
        • It defines a function getFileData to read and parse JSON data from files.
        • It reads different JSON files based on the options type and assigns the data to the variable db.
        • If the options type is not validate and the variable to check is all, it returns all APIs available.
        • If the options type is not validate and a specific variable to check is provided, it returns the data corresponding to that variable.
        • If the options type is validate and a specific variable to check is provided, it validates the request body against the schema retrieved from the database.
          • If the validation fails, it returns a 404 error along with the validation errors.
          • If the validation succeeds, it returns a 202 status indicating successful validation.
        • If the options type is validate but no specific variable to check is provided, or if the variable to check is not found, it returns a 404 error indicating an invalid variable.
⁠4.
  • JSON Badge Please refrain from making any changes to this file.

    • TMForum Open-API Sample API Collection Repository
  • JSON Badge Adjustments to the JSON file should adhere to the schema provided in the following example.

    • If make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved using lowdb.
    • Request body should be object enclosed, just like the GET output.
    • example :
      {
          "id": "db1d2ae3-5496-4040-8be9-6e3d4012ba0a",
          "href": "http://localhost:9000/tmf-api/v4/getAPI/db1d2ae3-5496-4040-8be9-6e3d4012ba0a"
      }
      
    • ID values are not mutable. Any id value in the body of your PUT or PATCH request will be ignored. Only a value set in a JSON Badge request will be respected, but only if not already taken.
    • APOST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON Badge in the request body. Otherwise it will return a 2XX status code, but without changes being made to the data.
  • JSON Badge Adjustments to the JSON file should adhere to the schema provided in the following example.

    • Help organize resources within JS Badge.
    • Facilitate the creation of a RESTful API structure.
    • Allow for endpoint customization to match application needs.
    • Enable filtering, sorting, and manipulation of data.
    • Integrate middleware for additional functionality.
    • Aid in implementing security measures like authentication and authorization.
    • Versioned to manage API changes.
    • example :
      {
      "/tmf-api/v4/*":"/$1",
      "/tmf-api/v5/*":"/$1"
      } 
      
  • JSON Badge Please refrain from making any changes to this file.

    • TMForum Open-API Schema Repository
  • JSON Badge Please refrain from making any changes to this file.

    • TMForum Open-API Swagger Repository

⁠License:

TM Forum Open API is licensed under MIT.

⁠Support

This is a self-developed project. There is no official tmforum support for this project.

Tag summary

Content type

Image

Digest

sha256:97d59f801…

Size

61.7 MB

Last updated

about 2 years ago

docker pull guna3006/tmfm:5.0.0