source: node_modules/swagger-client/lib/execute/oas3/style-serializer.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[d24f17c]1"use strict";
2
3exports.__esModule = true;
4exports.default = stylize;
5exports.encodeDisallowedCharacters = encodeDisallowedCharacters;
6const isRfc3986Reserved = char => ":/?#[]@!$&'()*+,;=".indexOf(char) > -1;
7const isRrc3986Unreserved = char => /^[a-z0-9\-._~]+$/i.test(char);
8
9// eslint-disable-next-line default-param-last
10function encodeDisallowedCharacters(str, {
11 escape
12} = {}, parse) {
13 if (typeof str === 'number') {
14 str = str.toString();
15 }
16 if (typeof str !== 'string' || !str.length) {
17 return str;
18 }
19 if (!escape) {
20 return str;
21 }
22 if (parse) {
23 return JSON.parse(str);
24 }
25
26 // In ES6 you can do this quite easily by using the new ... spread operator.
27 // This causes the string iterator (another new ES6 feature) to be used internally,
28 // and because that iterator is designed to deal with
29 // code points rather than UCS-2/UTF-16 code units.
30 return [...str].map(char => {
31 if (isRrc3986Unreserved(char)) {
32 return char;
33 }
34 if (isRfc3986Reserved(char) && escape === 'unsafe') {
35 return char;
36 }
37 const encoder = new TextEncoder();
38 const encoded = Array.from(encoder.encode(char)).map(byte => `0${byte.toString(16).toUpperCase()}`.slice(-2)).map(encodedByte => `%${encodedByte}`).join('');
39 return encoded;
40 }).join('');
41}
42function stylize(config) {
43 const {
44 value
45 } = config;
46 if (Array.isArray(value)) {
47 return encodeArray(config);
48 }
49 if (typeof value === 'object') {
50 return encodeObject(config);
51 }
52 return encodePrimitive(config);
53}
54function encodeArray({
55 key,
56 value,
57 style,
58 explode,
59 escape
60}) {
61 const valueEncoder = str => encodeDisallowedCharacters(str, {
62 escape
63 });
64 if (style === 'simple') {
65 return value.map(val => valueEncoder(val)).join(',');
66 }
67 if (style === 'label') {
68 return `.${value.map(val => valueEncoder(val)).join('.')}`;
69 }
70 if (style === 'matrix') {
71 return value.map(val => valueEncoder(val)).reduce((prev, curr) => {
72 if (!prev || explode) {
73 return `${prev || ''};${key}=${curr}`;
74 }
75 return `${prev},${curr}`;
76 }, '');
77 }
78 if (style === 'form') {
79 const after = explode ? `&${key}=` : ',';
80 return value.map(val => valueEncoder(val)).join(after);
81 }
82 if (style === 'spaceDelimited') {
83 const after = explode ? `${key}=` : '';
84 return value.map(val => valueEncoder(val)).join(` ${after}`);
85 }
86 if (style === 'pipeDelimited') {
87 const after = explode ? `${key}=` : '';
88 return value.map(val => valueEncoder(val)).join(`|${after}`);
89 }
90 return undefined;
91}
92function encodeObject({
93 key,
94 value,
95 style,
96 explode,
97 escape
98}) {
99 const valueEncoder = str => encodeDisallowedCharacters(str, {
100 escape
101 });
102 const valueKeys = Object.keys(value);
103 if (style === 'simple') {
104 return valueKeys.reduce((prev, curr) => {
105 const val = valueEncoder(value[curr]);
106 const middleChar = explode ? '=' : ',';
107 const prefix = prev ? `${prev},` : '';
108 return `${prefix}${curr}${middleChar}${val}`;
109 }, '');
110 }
111 if (style === 'label') {
112 return valueKeys.reduce((prev, curr) => {
113 const val = valueEncoder(value[curr]);
114 const middleChar = explode ? '=' : '.';
115 const prefix = prev ? `${prev}.` : '.';
116 return `${prefix}${curr}${middleChar}${val}`;
117 }, '');
118 }
119 if (style === 'matrix' && explode) {
120 return valueKeys.reduce((prev, curr) => {
121 const val = valueEncoder(value[curr]);
122 const prefix = prev ? `${prev};` : ';';
123 return `${prefix}${curr}=${val}`;
124 }, '');
125 }
126 if (style === 'matrix') {
127 // no explode
128 return valueKeys.reduce((prev, curr) => {
129 const val = valueEncoder(value[curr]);
130 const prefix = prev ? `${prev},` : `;${key}=`;
131 return `${prefix}${curr},${val}`;
132 }, '');
133 }
134 if (style === 'form') {
135 return valueKeys.reduce((prev, curr) => {
136 const val = valueEncoder(value[curr]);
137 const prefix = prev ? `${prev}${explode ? '&' : ','}` : '';
138 const separator = explode ? '=' : ',';
139 return `${prefix}${curr}${separator}${val}`;
140 }, '');
141 }
142 return undefined;
143}
144function encodePrimitive({
145 key,
146 value,
147 style,
148 escape
149}) {
150 const valueEncoder = str => encodeDisallowedCharacters(str, {
151 escape
152 });
153 if (style === 'simple') {
154 return valueEncoder(value);
155 }
156 if (style === 'label') {
157 return `.${valueEncoder(value)}`;
158 }
159 if (style === 'matrix') {
160 return `;${key}=${valueEncoder(value)}`;
161 }
162 if (style === 'form') {
163 return valueEncoder(value);
164 }
165 if (style === 'deepObject') {
166 return valueEncoder(value, {}, true);
167 }
168 return undefined;
169}
Note: See TracBrowser for help on using the repository browser.