2.3.2 • Published 7 months ago

@namcaodev/postman-codegen v2.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Postman Codegen

GitHub license Version Build Status

Postman Codegen is a tool that helps you automatically generate typescript files, tanstack queries easily from postman json

✨ Features

  • šŸ“Œ Support full feature codegen typescript, tanstack query for people
  • ⚔ Optimized performance and easy to use
  • šŸ”§ Easily integrates with TypeScript projects

šŸ“¦ Installation

Install via npm or yarn:

npm install @namcaodev/postman-codegen
# or
yarn add @namcaodev/postman-codegen

šŸš€ Usage

1. Create codegen.config.cjs

To configure the library, create a codegen.config.cjs file in your root project with the following examples options:

/**
 * @namcaodev/postman-codegen configuration.
 *
 * @type {import("@namcaodev/postman-codegen").CodegenConfig}
 */

module.exports = {
  generateType: 'seperate', // 'seperate' | 'combine',
  generateMode: 'json_file', // 'fetch' | 'json_file'
  postmanFetchConfigs: {
    collectionId: '<YOUR_COLLECTION_ID>',
    collectionAccessKey: '<YOUR_COLLECTION_ACCESS_KEY>'
  }, // Config for fetch Postman document (Require when generate mode is fetch)
  postmanJsonPath: "examples/postman-collection.json", // Postman Json Path (Require when generate mode is json_file)
  generateOutputPath: "examples/generated", // Generated Folder path
  propertyApiGetList: "items", // API response field containing list data
  enableZodGeneration: true, // Enable zod schema generation
  typeConfigs: {
    allPropertiesOptional: true, // Mark all properties as optional
    inferEnums: true, // inferEnums to string
    inferDateTimes: true, // inferDateTimes to string
  }, // Type configs for generate (Optional)
  fetcher: "../../../helpers/fetcher", // Link to your custom fetcher
  generateFileNames: {
    requestType: "apiRequests.ts",
    queryType: "apiQueries.ts",
    responseType: "apiResponses.ts",
    queryOptions: "query.ts",
    mutationOptions: "mutation.ts",
  }, // Customize generated file names (Optional)
};

2. Run the command

After adding the configuration file, run the following command to generate the code:

npx postman-codegen

The generated files will be saved in the folder specified in generateOutputPath (e.g., generated).

3. Usage in React Component

Next, easy usage in your React Component with full type safe power demo.tsx

import React from "react";
import { useQuery } from "@tanstack/react-query";
import {
  newmanGetRequestQueryOptions,
  newmanGetRequestQueryKeys,
} from "./generated/newman-get-request/query";

const DemoCodegen = () => {
  const query = useQuery({
    ...newmanGetRequestQueryOptions({
      source: "test",
    }),
  });
  const queryKeys = newmanGetRequestQueryKeys({ source: "test" });
  console.log("Data response", query);
  console.log("Query keys", queryKeys);
  return <div>DemoCodegen</div>;
};

export default DemoCodegen;

āš ļø Warning

With your custom fetcher you must follow the standard interface and function creation here:

export interface CustomFetchParams<TBody> {
  url: string;
  method: string;
  options?: RequestInit;
  body?: TBody;
}

export const customFetch = async <TResponseData = undefined, TBody = undefined>(
  params: CustomFetchParams<TBody>
): Promise<TResponseData> => {
  // You will handle your fetch here
};

export default customFetch; // You must be export default it

You can see an example in the source code here: fetcher.ts and how it is handled here: queryWithParams.hbs.

šŸ”§ Configuration Options

OptionTypeRequiredDescription
generateType'seperate' \| 'combine'āŒ No (Default: seperate)'separate': Generates separate folders/files for each API path. 'combine': Merges all types and queries into a single file.
generateMode'fetch' \| 'json_file'āœ… YesDetermines how data is retrieved from Postman. 'fetch' fetches the collection via API, while 'json_file' uses an exported JSON file.
postmanFetchConfigs.collectionIdstringšŸ”¹ If generateMode = 'fetch'The Postman Collection ID to fetch.
postmanFetchConfigs.collectionAccessKeystringšŸ”¹ If generateMode = 'fetch'The API Key required to access the collection from Postman API.
postmanJsonPathstringšŸ”¹ If generateMode = 'json_file'Path to the exported Postman JSON file.
generateOutputPathstringāœ… YesDirectory where the generated files will be stored.
propertyApiGetListstringāœ… YesThe key containing the list of data in the API response.
enableZodGenerationbooleanāŒ No (Default: false)If true, automatically generates Zod schemas.
typeConfigs.allPropertiesOptionalbooleanāŒ No (Default: false)If true, all properties will be optional.
typeConfigs.inferEnumsbooleanāŒ No (Default: false)If true, automatically infers enums from API responses.
typeConfigs.inferDateTimesbooleanāŒ No (Default: false)If true, automatically detects datetime fields in responses.
fetcherstringāœ… YesPath to a custom fetcher function.
generateFileNames.requestTypestringāŒ No (Default: apiRequests.ts)Filename for API request definitions.
generateFileNames.queryTypestringāŒ No (Default: apiQueries.ts)Filename for API queries.
generateFileNames.responseTypestringāŒ No (Default: apiResponses.ts)Filename for API response types.
generateFileNames.queryOptionsstringāŒ No (Default: query.ts)Filename for query options.
generateFileNames.mutationOptionsstringāŒ No (Default: mutation.ts)Filename for mutation options.

šŸ“Œ Notes:

  • If generateMode = 'fetch', both postmanFetchConfigs.collectionId and postmanFetchConfigs.collectionAccessKey are required.

  • If generateMode = 'json_file', postmanJsonPath is required.

  • ⚔ Using generateMode = 'fetch' allows you to sync updates from your backend in Postman documents instantly.

šŸ›  Contributing

If you want to contribute, please fork the repository and submit a pull request!

git clone https://github.com/NamCaoDev/postman-codegen.git
cd postman-codegen
npm install
npx postman-codegen

šŸ“œ License

Distributed under the MIT License. See the LICENSE file for more details.

šŸŒ Contact

  • šŸ“§ Email: namcaodev@gmail.com
  • 🐦 Twitter: @namcaodev

Thank you for using Postman Codegen Library! šŸš€

2.3.2

7 months ago

2.3.1

7 months ago

2.3.0

7 months ago

2.2.9

7 months ago

2.2.8

7 months ago

2.2.7

7 months ago

2.2.6

7 months ago

2.2.5

7 months ago

2.2.4

7 months ago

2.2.3

7 months ago

2.2.0

7 months ago

2.1.0

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

8 months ago

0.4.2

8 months ago

0.3.2

8 months ago

0.2.9

8 months ago

0.2.8

8 months ago

0.2.6

8 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago