Is it possible to generate Typescript interfaces from files with a webpack loader?

前端 未结 2 979

I am attempting to create a webpack loader that converts a file containing a description of API data structures into a set of TypeScript interfaces.

In my concrete case,

2条回答
  •  情书的邮戳
    2021-02-19 22:34

    If your API follows the swagger spec, you can use the npm package swagger-ts-generator to generate TypeScript files from it:

    Swagger TypeScript code generator

    Node module to generate TypeScript code for Angular (2 and above) based on Webapi meta data in Swagger v2 format.

    Basically, you give it the swagger URL and it generates TypeScript. The examples are for Gulp, but they should port over to WebPack fairly well:

    var swagger = {
        url: 'http://petstore.swagger.io/v2/swagger.json',
        //url: 'http://127.0.0.1/ZIB.WebApi.v2/swagger/docs/v1',
        swaggerFile: folders.swaggerFolder + files.swaggerJson,
        swaggerFolder: folders.swaggerFolder,
        swaggerTSGeneratorOptions: {
            modelFolder: folders.srcWebapiFolder,
            enumTSFile: folders.srcWebapiFolder + 'enums.ts',
            enumLanguageFiles: [
                folders.srcLanguagesFolder + 'nl.json',
                folders.srcLanguagesFolder + 'en.json',
            ],
            modelModuleName: 'webapi.models',
            enumModuleName: 'webapi.enums',
            enumRef: './enums',
            namespacePrefixesToRemove: [
            ],
            typeNameSuffixesToRemove: [
            ]
        }
    }
    

提交回复
热议问题