fork of hey-api/openapi-ts because I need some additional things
1import type { Context } from './context';
2import type { Pagination } from './pagination';
3import type { IR } from './types';
4
5const getPaginationSchema = ({
6 context,
7 parameter,
8}: {
9 context: Context;
10 parameter: IR.ParameterObject;
11}): IR.SchemaObject | undefined => {
12 if (!parameter.pagination) {
13 return;
14 }
15
16 if (parameter.pagination === true) {
17 return parameter.schema;
18 }
19
20 let schema = parameter.schema;
21 if (schema.$ref) {
22 schema = context.resolveIrRef<IR.SchemaObject>(schema.$ref);
23 }
24
25 return schema.properties![parameter.pagination]!;
26};
27
28export const hasParameterGroupObjectRequired = (
29 parameterGroup?: Record<string, IR.ParameterObject>,
30): boolean => {
31 for (const name in parameterGroup) {
32 if (parameterGroup[name]!.required) {
33 return true;
34 }
35 }
36
37 return false;
38};
39
40export const hasParametersObjectRequired = (
41 parameters: IR.ParametersObject | undefined,
42): boolean => {
43 if (!parameters) {
44 return false;
45 }
46
47 if (hasParameterGroupObjectRequired(parameters.cookie)) {
48 return true;
49 }
50
51 if (hasParameterGroupObjectRequired(parameters.header)) {
52 return true;
53 }
54
55 if (hasParameterGroupObjectRequired(parameters.path)) {
56 return true;
57 }
58
59 if (hasParameterGroupObjectRequired(parameters.query)) {
60 return true;
61 }
62
63 return false;
64};
65
66export const parameterWithPagination = ({
67 context,
68 parameters,
69}: {
70 context: Context;
71 parameters: IR.ParametersObject | undefined;
72}): Pagination | undefined => {
73 if (!parameters) {
74 return;
75 }
76
77 for (const name in parameters.cookie) {
78 const parameter = parameters.cookie[name]!;
79 if (parameter.pagination) {
80 return {
81 in: parameter.location,
82 name:
83 parameter.pagination === true
84 ? parameter.name
85 : `${parameter.name}.${parameter.pagination}`,
86 schema: getPaginationSchema({ context, parameter })!,
87 };
88 }
89 }
90
91 for (const name in parameters.header) {
92 const parameter = parameters.header[name]!;
93 if (parameter.pagination) {
94 return {
95 in: parameter.location,
96 name:
97 parameter.pagination === true
98 ? parameter.name
99 : `${parameter.name}.${parameter.pagination}`,
100 schema: getPaginationSchema({ context, parameter })!,
101 };
102 }
103 }
104
105 for (const name in parameters.path) {
106 const parameter = parameters.path[name]!;
107 if (parameter.pagination) {
108 return {
109 in: parameter.location,
110 name:
111 parameter.pagination === true
112 ? parameter.name
113 : `${parameter.name}.${parameter.pagination}`,
114 schema: getPaginationSchema({ context, parameter })!,
115 };
116 }
117 }
118
119 for (const name in parameters.query) {
120 const parameter = parameters.query[name]!;
121 if (parameter.pagination) {
122 return {
123 in: parameter.location,
124 name:
125 parameter.pagination === true
126 ? parameter.name
127 : `${parameter.name}.${parameter.pagination}`,
128 schema: getPaginationSchema({ context, parameter })!,
129 };
130 }
131 }
132
133 return;
134};