@kubb/swagger-ts 2.0.0-beta.5 → 2.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { createPlugin, FileManager, PluginManager, Generator } from '@kubb/core'
4
4
  import transformers, { camelCase, pascalCase } from '@kubb/core/transformers';
5
5
  import { renderTemplate, getUniqueName } from '@kubb/core/utils';
6
6
  import { pluginName as pluginName$1, OasBuilder, OperationGenerator as OperationGenerator$1 } from '@kubb/swagger';
7
- import { createRoot, usePlugin, usePluginManager, File } from '@kubb/react';
7
+ import { createRoot, usePlugin, usePluginManager, File, useFile, Type } from '@kubb/react';
8
8
  import { print } from '@kubb/parser';
9
9
  import * as factory from '@kubb/parser/factory';
10
10
  import { keywordTypeNodes } from '@kubb/parser/factory';
@@ -3141,11 +3141,11 @@ var require_react_jsx_runtime_development = __commonJS({
3141
3141
  return jsxWithValidation(type, props, key, false);
3142
3142
  }
3143
3143
  }
3144
- var jsx4 = jsxWithValidationDynamic;
3145
- var jsxs3 = jsxWithValidationStatic;
3144
+ var jsx5 = jsxWithValidationDynamic;
3145
+ var jsxs4 = jsxWithValidationStatic;
3146
3146
  exports.Fragment = REACT_FRAGMENT_TYPE;
3147
- exports.jsx = jsx4;
3148
- exports.jsxs = jsxs3;
3147
+ exports.jsx = jsx5;
3148
+ exports.jsxs = jsxs4;
3149
3149
  })();
3150
3150
  }
3151
3151
  }
@@ -3598,9 +3598,51 @@ Mutation.File = function({ mode }) {
3598
3598
  ) });
3599
3599
  };
3600
3600
 
3601
- // src/components/Query.tsx
3601
+ // src/components/Oas.tsx
3602
3602
  init_esm_shims();
3603
3603
  var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
3604
+ function Template({
3605
+ name,
3606
+ typeName,
3607
+ api
3608
+ }) {
3609
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
3610
+ `export const ${name} = ${JSON.stringify(api, void 0, 2)} as const`,
3611
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("br", {}),
3612
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Type, { name: typeName, export: true, children: `Parse<typeof ${name}>` })
3613
+ ] });
3614
+ }
3615
+ var defaultTemplates = { default: Template };
3616
+ function Oas({
3617
+ name,
3618
+ typeName,
3619
+ Template: Template2 = defaultTemplates.default
3620
+ }) {
3621
+ const oas = useOas();
3622
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Template2, { name, typeName, api: oas.api });
3623
+ }
3624
+ Oas.File = function({ name, typeName, templates = defaultTemplates }) {
3625
+ const { key: pluginKey2 } = usePlugin();
3626
+ const file = useFile({ name, pluginKey: pluginKey2 });
3627
+ const Template2 = templates.default;
3628
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
3629
+ File,
3630
+ {
3631
+ baseName: file.baseName,
3632
+ path: file.path,
3633
+ meta: file.meta,
3634
+ children: [
3635
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(File.Import, { name: ["Parse"], path: "@kubb/swagger-ts/infer", isTypeOnly: true }),
3636
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(File.Source, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Oas, { Template: Template2, name, typeName }) })
3637
+ ]
3638
+ }
3639
+ );
3640
+ };
3641
+ Oas.templates = defaultTemplates;
3642
+
3643
+ // src/components/Query.tsx
3644
+ init_esm_shims();
3645
+ var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
3604
3646
  function printCombinedSchema2(name, operation, schemas) {
3605
3647
  const properties = {
3606
3648
  "response": factory.createTypeReferenceNode(
@@ -3662,7 +3704,7 @@ function Query({
3662
3704
  builder
3663
3705
  }) {
3664
3706
  const { source } = builder.build();
3665
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: source });
3707
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: source });
3666
3708
  }
3667
3709
  Query.File = function({ mode }) {
3668
3710
  const { options } = usePlugin();
@@ -3674,7 +3716,7 @@ Query.File = function({ mode }) {
3674
3716
  const operation = useOperation();
3675
3717
  const builder = new TypeBuilder(options, { oas, pluginManager }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.response).add(schemas.errors);
3676
3718
  const { source, imports } = builder.build();
3677
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
3719
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
3678
3720
  File,
3679
3721
  {
3680
3722
  baseName: file.baseName,
@@ -3682,9 +3724,9 @@ Query.File = function({ mode }) {
3682
3724
  meta: file.meta,
3683
3725
  children: [
3684
3726
  mode === "directory" && imports.map((item, index) => {
3685
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(File.Import, { root: file.path, ...item }, index);
3727
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(File.Import, { root: file.path, ...item }, index);
3686
3728
  }),
3687
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(File.Source, { children: [
3729
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(File.Source, { children: [
3688
3730
  source,
3689
3731
  printCombinedSchema2(factoryName, operation, schemas)
3690
3732
  ] })
@@ -3694,16 +3736,25 @@ Query.File = function({ mode }) {
3694
3736
  };
3695
3737
 
3696
3738
  // src/OperationGenerator.tsx
3697
- var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
3739
+ var import_jsx_runtime4 = __toESM(require_jsx_runtime(), 1);
3698
3740
  var OperationGenerator = class extends OperationGenerator$1 {
3699
3741
  async all() {
3700
- return null;
3742
+ const { oas, pluginManager, plugin } = this.context;
3743
+ if (!plugin.options.oasType) {
3744
+ return null;
3745
+ }
3746
+ const root = createRoot({ logger: pluginManager.logger });
3747
+ root.render(
3748
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Oas.File, { name: "oas", typeName: "Oas" }),
3749
+ { meta: { oas, pluginManager, plugin } }
3750
+ );
3751
+ return root.files;
3701
3752
  }
3702
3753
  async get(operation, schemas, options) {
3703
3754
  const { oas, pluginManager, plugin, mode = "directory" } = this.context;
3704
3755
  const root = createRoot({ logger: pluginManager.logger });
3705
3756
  root.render(
3706
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Query.File, { mode }),
3757
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Query.File, { mode }),
3707
3758
  { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } }
3708
3759
  );
3709
3760
  return root.files;
@@ -3712,7 +3763,7 @@ var OperationGenerator = class extends OperationGenerator$1 {
3712
3763
  const { oas, pluginManager, plugin, mode = "directory" } = this.context;
3713
3764
  const root = createRoot({ logger: pluginManager.logger });
3714
3765
  root.render(
3715
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Mutation.File, { mode }),
3766
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Mutation.File, { mode }),
3716
3767
  { meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } }
3717
3768
  );
3718
3769
  return root.files;
@@ -3733,7 +3784,7 @@ var pluginName = "swagger-ts";
3733
3784
  var pluginKey = [pluginName];
3734
3785
  var definePlugin = createPlugin((options) => {
3735
3786
  const {
3736
- output = "types",
3787
+ output = { path: "types" },
3737
3788
  group,
3738
3789
  exclude = [],
3739
3790
  include,
@@ -3742,9 +3793,9 @@ var definePlugin = createPlugin((options) => {
3742
3793
  dateType = "string",
3743
3794
  optionalType = "questionToken",
3744
3795
  transformers: transformers5 = {},
3745
- exportAs
3796
+ oasType = false
3746
3797
  } = options;
3747
- const template = group?.output ? group.output : `${output}/{{tag}}Controller`;
3798
+ const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
3748
3799
  return {
3749
3800
  name: pluginName,
3750
3801
  options: {
@@ -3752,21 +3803,22 @@ var definePlugin = createPlugin((options) => {
3752
3803
  dateType,
3753
3804
  enumType,
3754
3805
  optionalType,
3806
+ oasType,
3755
3807
  // keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
3756
3808
  usedEnumNames: {}
3757
3809
  },
3758
3810
  pre: [pluginName$1],
3759
3811
  resolvePath(baseName, directory, options2) {
3760
3812
  const root = path.resolve(this.config.root, this.config.output.path);
3761
- const mode = FileManager.getMode(path.resolve(root, output));
3813
+ const mode = FileManager.getMode(path.resolve(root, output.path));
3762
3814
  if (mode === "file") {
3763
- return path.resolve(root, output);
3815
+ return path.resolve(root, output.path);
3764
3816
  }
3765
3817
  if (options2?.tag && group?.type === "tag") {
3766
3818
  const tag = camelCase(options2.tag);
3767
3819
  return path.resolve(root, renderTemplate(template, { tag }), baseName);
3768
3820
  }
3769
- return path.resolve(root, output, baseName);
3821
+ return path.resolve(root, output.path, baseName);
3770
3822
  },
3771
3823
  resolveName(name, type) {
3772
3824
  const resolvedName = pascalCase(name);
@@ -3786,7 +3838,7 @@ var definePlugin = createPlugin((options) => {
3786
3838
  const oas = await swaggerPlugin.api.getOas();
3787
3839
  const schemas = await swaggerPlugin.api.getSchemas();
3788
3840
  const root = path.resolve(this.config.root, this.config.output.path);
3789
- const mode = FileManager.getMode(path.resolve(root, output));
3841
+ const mode = FileManager.getMode(path.resolve(root, output.path));
3790
3842
  const builder = new TypeBuilder(this.plugin.options, { oas, pluginManager: this.pluginManager });
3791
3843
  builder.add(
3792
3844
  Object.entries(schemas).map(([name, schema]) => ({ name, schema }))
@@ -3820,7 +3872,7 @@ var definePlugin = createPlugin((options) => {
3820
3872
  }
3821
3873
  await this.addFile({
3822
3874
  path: resolvedPath,
3823
- baseName: output,
3875
+ baseName: output.path,
3824
3876
  source,
3825
3877
  imports: [],
3826
3878
  meta: {
@@ -3851,25 +3903,10 @@ var definePlugin = createPlugin((options) => {
3851
3903
  const root = path.resolve(this.config.root, this.config.output.path);
3852
3904
  await this.fileManager.addIndexes({
3853
3905
  root,
3906
+ output,
3854
3907
  extName: ".ts",
3855
3908
  meta: { pluginKey: this.plugin.key },
3856
3909
  options: {
3857
- map: (file) => {
3858
- return {
3859
- ...file,
3860
- exports: file.exports?.map((item) => {
3861
- if (exportAs) {
3862
- return {
3863
- ...item,
3864
- name: exportAs,
3865
- asAlias: !!exportAs
3866
- };
3867
- }
3868
- return item;
3869
- })
3870
- };
3871
- },
3872
- output,
3873
3910
  isTypeOnly: true
3874
3911
  }
3875
3912
  });
@@ -3877,6 +3914,13 @@ var definePlugin = createPlugin((options) => {
3877
3914
  };
3878
3915
  });
3879
3916
 
3917
+ // src/types.ts
3918
+ init_esm_shims();
3919
+
3920
+ // src/infer/index.ts
3921
+ var infer_exports = {};
3922
+ init_esm_shims();
3923
+
3880
3924
  // src/index.ts
3881
3925
  var src_default = definePlugin;
3882
3926
  /*! Bundled license information:
@@ -3926,6 +3970,6 @@ react/cjs/react-jsx-runtime.development.js:
3926
3970
  *)
3927
3971
  */
3928
3972
 
3929
- export { src_default as default, definePlugin, pluginKey, pluginName };
3973
+ export { infer_exports as Infer, src_default as default, definePlugin, pluginKey, pluginName };
3930
3974
  //# sourceMappingURL=out.js.map
3931
3975
  //# sourceMappingURL=index.js.map