@usebruno/filestore 0.4.0 → 0.6.0

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.
Files changed (157) hide show
  1. package/dist/cjs/index.js +1 -1
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/workers/worker-script.js +1 -1
  4. package/dist/cjs/workers/worker-script.js.map +1 -1
  5. package/dist/esm/index.js +1 -1
  6. package/dist/esm/index.js.map +1 -1
  7. package/dist/esm/workers/worker-script.js +1 -1
  8. package/dist/esm/workers/worker-script.js.map +1 -1
  9. package/dist/formats/bru/index.d.ts +20 -0
  10. package/dist/formats/bru/index.d.ts.map +1 -0
  11. package/dist/formats/bru/tests/fixtures/oauth2-additional-params.d.ts +57 -0
  12. package/dist/formats/bru/tests/fixtures/oauth2-additional-params.d.ts.map +1 -0
  13. package/dist/formats/bru/tests/oauth2-additional-params.spec.d.ts +2 -0
  14. package/dist/formats/bru/tests/oauth2-additional-params.spec.d.ts.map +1 -0
  15. package/dist/formats/bru/tests/request-parse-and-redact-body-data.spec.d.ts +2 -0
  16. package/dist/formats/bru/tests/request-parse-and-redact-body-data.spec.d.ts.map +1 -0
  17. package/dist/{esm/formats → formats}/bru/utils/oauth2-additional-params.d.ts +1 -0
  18. package/dist/formats/bru/utils/oauth2-additional-params.d.ts.map +1 -0
  19. package/dist/{cjs/workers/formats → formats}/bru/utils/request-parse-and-redact-body-data.d.ts +1 -0
  20. package/dist/formats/bru/utils/request-parse-and-redact-body-data.d.ts.map +1 -0
  21. package/dist/formats/yml/common/actions.d.ts +14 -0
  22. package/dist/formats/yml/common/actions.d.ts.map +1 -0
  23. package/dist/formats/yml/common/assertions.d.ts +5 -0
  24. package/dist/formats/yml/common/assertions.d.ts.map +1 -0
  25. package/dist/formats/yml/common/auth-oauth2.d.ts +5 -0
  26. package/dist/formats/yml/common/auth-oauth2.d.ts.map +1 -0
  27. package/dist/formats/yml/common/auth.d.ts +5 -0
  28. package/dist/formats/yml/common/auth.d.ts.map +1 -0
  29. package/dist/formats/yml/common/body.d.ts +5 -0
  30. package/dist/formats/yml/common/body.d.ts.map +1 -0
  31. package/dist/formats/yml/common/headers.d.ts +7 -0
  32. package/dist/formats/yml/common/headers.d.ts.map +1 -0
  33. package/dist/formats/yml/common/params.d.ts +5 -0
  34. package/dist/formats/yml/common/params.d.ts.map +1 -0
  35. package/dist/formats/yml/common/scripts.d.ts +14 -0
  36. package/dist/formats/yml/common/scripts.d.ts.map +1 -0
  37. package/dist/formats/yml/common/variables.d.ts +17 -0
  38. package/dist/formats/yml/common/variables.d.ts.map +1 -0
  39. package/dist/formats/yml/index.d.ts +10 -0
  40. package/dist/formats/yml/index.d.ts.map +1 -0
  41. package/dist/formats/yml/items/parseGraphQLRequest.d.ts +5 -0
  42. package/dist/formats/yml/items/parseGraphQLRequest.d.ts.map +1 -0
  43. package/dist/formats/yml/items/parseGrpcRequest.d.ts +5 -0
  44. package/dist/formats/yml/items/parseGrpcRequest.d.ts.map +1 -0
  45. package/dist/formats/yml/items/parseHttpRequest.d.ts +5 -0
  46. package/dist/formats/yml/items/parseHttpRequest.d.ts.map +1 -0
  47. package/dist/formats/yml/items/parseScript.d.ts +5 -0
  48. package/dist/formats/yml/items/parseScript.d.ts.map +1 -0
  49. package/dist/formats/yml/items/parseWebsocketRequest.d.ts +5 -0
  50. package/dist/formats/yml/items/parseWebsocketRequest.d.ts.map +1 -0
  51. package/dist/formats/yml/items/stringifyGraphQLRequest.d.ts +4 -0
  52. package/dist/formats/yml/items/stringifyGraphQLRequest.d.ts.map +1 -0
  53. package/dist/formats/yml/items/stringifyGrpcRequest.d.ts +4 -0
  54. package/dist/formats/yml/items/stringifyGrpcRequest.d.ts.map +1 -0
  55. package/dist/formats/yml/items/stringifyHttpRequest.d.ts +4 -0
  56. package/dist/formats/yml/items/stringifyHttpRequest.d.ts.map +1 -0
  57. package/dist/formats/yml/items/stringifyScript.d.ts +4 -0
  58. package/dist/formats/yml/items/stringifyScript.d.ts.map +1 -0
  59. package/dist/formats/yml/items/stringifyWebsocketRequest.d.ts +4 -0
  60. package/dist/formats/yml/items/stringifyWebsocketRequest.d.ts.map +1 -0
  61. package/dist/formats/yml/parseCollection.d.ts +8 -0
  62. package/dist/formats/yml/parseCollection.d.ts.map +1 -0
  63. package/dist/formats/yml/parseEnvironment.d.ts +4 -0
  64. package/dist/formats/yml/parseEnvironment.d.ts.map +1 -0
  65. package/dist/formats/yml/parseFolder.d.ts +4 -0
  66. package/dist/formats/yml/parseFolder.d.ts.map +1 -0
  67. package/dist/formats/yml/parseItem.d.ts +4 -0
  68. package/dist/formats/yml/parseItem.d.ts.map +1 -0
  69. package/dist/formats/yml/stringifyCollection.d.ts +3 -0
  70. package/dist/formats/yml/stringifyCollection.d.ts.map +1 -0
  71. package/dist/formats/yml/stringifyEnvironment.d.ts +4 -0
  72. package/dist/formats/yml/stringifyEnvironment.d.ts.map +1 -0
  73. package/dist/formats/yml/stringifyFolder.d.ts +4 -0
  74. package/dist/formats/yml/stringifyFolder.d.ts.map +1 -0
  75. package/dist/formats/yml/stringifyItem.d.ts +4 -0
  76. package/dist/formats/yml/stringifyItem.d.ts.map +1 -0
  77. package/dist/formats/yml/utils.d.ts +3 -0
  78. package/dist/formats/yml/utils.d.ts.map +1 -0
  79. package/dist/{cjs/workers/index.d.ts → index.d.ts} +13 -6
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/types.d.ts +19 -0
  82. package/dist/types.d.ts.map +1 -0
  83. package/dist/utils/index.d.ts +5 -0
  84. package/dist/utils/index.d.ts.map +1 -0
  85. package/dist/{esm/workers → workers}/WorkerQueue/index.d.ts +1 -0
  86. package/dist/workers/WorkerQueue/index.d.ts.map +1 -0
  87. package/dist/workers/index.d.ts +12 -0
  88. package/dist/workers/index.d.ts.map +1 -0
  89. package/dist/workers/worker-script.d.ts +2 -0
  90. package/dist/workers/worker-script.d.ts.map +1 -0
  91. package/package.json +12 -6
  92. package/src/formats/bru/index.ts +103 -11
  93. package/src/formats/bru/tests/oauth2-additional-params.spec.js +1 -1
  94. package/src/formats/bru/utils/oauth2-additional-params.ts +17 -18
  95. package/src/formats/bru/utils/request-parse-and-redact-body-data.ts +13 -13
  96. package/src/formats/yml/common/actions.ts +78 -0
  97. package/src/formats/yml/common/assertions.ts +146 -0
  98. package/src/formats/yml/common/auth-oauth2.ts +555 -0
  99. package/src/formats/yml/common/auth.ts +245 -0
  100. package/src/formats/yml/common/body.ts +238 -0
  101. package/src/formats/yml/common/headers.ts +58 -0
  102. package/src/formats/yml/common/params.ts +57 -0
  103. package/src/formats/yml/common/scripts.ts +64 -0
  104. package/src/formats/yml/common/variables.ts +68 -0
  105. package/src/formats/yml/index.ts +20 -0
  106. package/src/formats/yml/items/parseGraphQLRequest.ts +137 -0
  107. package/src/formats/yml/items/parseGrpcRequest.ts +116 -0
  108. package/src/formats/yml/items/parseHttpRequest.ts +195 -0
  109. package/src/formats/yml/items/parseScript.ts +25 -0
  110. package/src/formats/yml/items/parseWebsocketRequest.ts +106 -0
  111. package/src/formats/yml/items/stringifyGraphQLRequest.ts +168 -0
  112. package/src/formats/yml/items/stringifyGrpcRequest.ts +137 -0
  113. package/src/formats/yml/items/stringifyHttpRequest.ts +221 -0
  114. package/src/formats/yml/items/stringifyScript.ts +22 -0
  115. package/src/formats/yml/items/stringifyWebsocketRequest.ts +114 -0
  116. package/src/formats/yml/parseCollection.ts +198 -0
  117. package/src/formats/yml/parseEnvironment.ts +56 -0
  118. package/src/formats/yml/parseFolder.ts +88 -0
  119. package/src/formats/yml/parseItem.ts +102 -0
  120. package/src/formats/yml/stringifyCollection.ts +230 -0
  121. package/src/formats/yml/stringifyEnvironment.ts +64 -0
  122. package/src/formats/yml/stringifyFolder.ts +96 -0
  123. package/src/formats/yml/stringifyItem.ts +37 -0
  124. package/src/formats/yml/utils.ts +38 -0
  125. package/src/index.ts +57 -27
  126. package/src/types/bruno-lang.d.ts +1 -1
  127. package/src/types.ts +5 -124
  128. package/src/utils/index.ts +15 -0
  129. package/src/workers/WorkerQueue/index.ts +10 -10
  130. package/src/workers/index.ts +20 -20
  131. package/src/workers/worker-script.ts +20 -6
  132. package/dist/cjs/formats/bru/index.d.ts +0 -6
  133. package/dist/cjs/formats/bru/utils/oauth2-additional-params.d.ts +0 -38
  134. package/dist/cjs/formats/bru/utils/request-parse-and-redact-body-data.d.ts +0 -9
  135. package/dist/cjs/index.d.ts +0 -16
  136. package/dist/cjs/types.d.ts +0 -142
  137. package/dist/cjs/workers/WorkerQueue/index.d.ts +0 -26
  138. package/dist/cjs/workers/formats/bru/index.d.ts +0 -6
  139. package/dist/cjs/workers/formats/bru/utils/oauth2-additional-params.d.ts +0 -38
  140. package/dist/cjs/workers/types.d.ts +0 -142
  141. package/dist/cjs/workers/worker-script.d.ts +0 -1
  142. package/dist/cjs/workers/workers/WorkerQueue/index.d.ts +0 -26
  143. package/dist/cjs/workers/workers/index.d.ts +0 -10
  144. package/dist/cjs/workers/workers/worker-script.d.ts +0 -1
  145. package/dist/esm/formats/bru/index.d.ts +0 -6
  146. package/dist/esm/formats/bru/utils/request-parse-and-redact-body-data.d.ts +0 -9
  147. package/dist/esm/index.d.ts +0 -16
  148. package/dist/esm/types.d.ts +0 -142
  149. package/dist/esm/workers/formats/bru/index.d.ts +0 -6
  150. package/dist/esm/workers/formats/bru/utils/oauth2-additional-params.d.ts +0 -38
  151. package/dist/esm/workers/formats/bru/utils/request-parse-and-redact-body-data.d.ts +0 -9
  152. package/dist/esm/workers/index.d.ts +0 -16
  153. package/dist/esm/workers/types.d.ts +0 -142
  154. package/dist/esm/workers/worker-script.d.ts +0 -1
  155. package/dist/esm/workers/workers/WorkerQueue/index.d.ts +0 -26
  156. package/dist/esm/workers/workers/index.d.ts +0 -10
  157. package/dist/esm/workers/workers/worker-script.d.ts +0 -1
@@ -0,0 +1,168 @@
1
+ import type { Item as BrunoItem, HttpItemSettings as BrunoHttpItemSettings } from '@usebruno/schema-types/collection/item';
2
+ import type { HttpRequest as BrunoHttpRequest } from '@usebruno/schema-types/requests/http';
3
+ import type { GraphQLRequest, GraphQLRequestSettings, GraphQLBody, GraphQLRequestInfo, GraphQLRequestDetails, GraphQLRequestRuntime } from '@opencollection/types/requests/graphql';
4
+ import type { Auth } from '@opencollection/types/common/auth';
5
+ import type { Scripts } from '@opencollection/types/common/scripts';
6
+ import type { Variable } from '@opencollection/types/common/variables';
7
+ import type { Assertion } from '@opencollection/types/common/assertions';
8
+ import type { Action } from '@opencollection/types/common/actions';
9
+ import type { HttpRequestParam, HttpRequestHeader } from '@opencollection/types/requests/http';
10
+ import { stringifyYml } from '../utils';
11
+ import { isNonEmptyString, isNumber } from '../../../utils';
12
+ import { toOpenCollectionAuth } from '../common/auth';
13
+ import { toOpenCollectionHttpHeaders } from '../common/headers';
14
+ import { toOpenCollectionParams } from '../common/params';
15
+ import { toOpenCollectionVariables } from '../common/variables';
16
+ import { toOpenCollectionActions } from '../common/actions';
17
+ import { toOpenCollectionScripts } from '../common/scripts';
18
+ import { toOpenCollectionAssertions } from '../common/assertions';
19
+
20
+ const stringifyGraphQLRequest = (item: BrunoItem): string => {
21
+ try {
22
+ const ocRequest: GraphQLRequest = {};
23
+ const brunoRequest = item.request as BrunoHttpRequest;
24
+
25
+ // info block
26
+ const info: GraphQLRequestInfo = {
27
+ name: isNonEmptyString(item.name) ? item.name : 'Untitled Request',
28
+ type: 'graphql'
29
+ };
30
+ if (item.seq) {
31
+ info.seq = item.seq;
32
+ }
33
+ if (item.tags?.length) {
34
+ info.tags = item.tags;
35
+ }
36
+ ocRequest.info = info;
37
+
38
+ // graphql block
39
+ const graphql: GraphQLRequestDetails = {
40
+ method: isNonEmptyString(brunoRequest.method) ? brunoRequest.method : 'POST',
41
+ url: isNonEmptyString(brunoRequest.url) ? brunoRequest.url : ''
42
+ };
43
+
44
+ // headers
45
+ const headers: HttpRequestHeader[] | undefined = toOpenCollectionHttpHeaders(brunoRequest.headers);
46
+ if (headers) {
47
+ graphql.headers = headers;
48
+ }
49
+
50
+ // params
51
+ const params: HttpRequestParam[] | undefined = toOpenCollectionParams(brunoRequest.params);
52
+ if (params) {
53
+ graphql.params = params;
54
+ }
55
+
56
+ // body
57
+ if (brunoRequest.body?.mode === 'graphql' && brunoRequest.body.graphql) {
58
+ const graphqlBody: GraphQLBody = {};
59
+ let hasBody = false;
60
+
61
+ if (isNonEmptyString(brunoRequest.body.graphql.query)) {
62
+ graphqlBody.query = brunoRequest.body.graphql.query;
63
+ hasBody = true;
64
+ }
65
+
66
+ if (isNonEmptyString(brunoRequest.body.graphql.variables)) {
67
+ graphqlBody.variables = brunoRequest.body.graphql.variables;
68
+ hasBody = true;
69
+ }
70
+
71
+ if (hasBody) {
72
+ graphql.body = graphqlBody;
73
+ }
74
+ }
75
+
76
+ // auth (in graphql block, not runtime)
77
+ const auth: Auth | undefined = toOpenCollectionAuth(brunoRequest.auth);
78
+ if (auth) {
79
+ graphql.auth = auth;
80
+ }
81
+
82
+ ocRequest.graphql = graphql;
83
+
84
+ // runtime block
85
+ const runtime: GraphQLRequestRuntime = {};
86
+ let hasRuntime = false;
87
+
88
+ // variables
89
+ const variables: Variable[] | undefined = toOpenCollectionVariables(brunoRequest.vars);
90
+ if (variables) {
91
+ runtime.variables = variables;
92
+ hasRuntime = true;
93
+ }
94
+
95
+ // scripts
96
+ const scripts: Scripts | undefined = toOpenCollectionScripts(brunoRequest);
97
+ if (scripts) {
98
+ runtime.scripts = scripts;
99
+ hasRuntime = true;
100
+ }
101
+
102
+ // assertions
103
+ const assertions: Assertion[] | undefined = toOpenCollectionAssertions(brunoRequest.assertions);
104
+ if (assertions) {
105
+ runtime.assertions = assertions;
106
+ hasRuntime = true;
107
+ }
108
+
109
+ // actions (from post-response variables)
110
+ const resVars = brunoRequest.vars?.res;
111
+ const actions: Action[] | undefined = toOpenCollectionActions(resVars);
112
+ if (actions) {
113
+ runtime.actions = actions;
114
+ hasRuntime = true;
115
+ }
116
+
117
+ if (hasRuntime) {
118
+ ocRequest.runtime = runtime;
119
+ }
120
+
121
+ // settings
122
+ const httpSettings = item.settings as BrunoHttpItemSettings | undefined;
123
+ const settings: GraphQLRequestSettings = {};
124
+ if (httpSettings?.encodeUrl === true) {
125
+ settings.encodeUrl = true;
126
+ } else if (httpSettings?.encodeUrl === false) {
127
+ settings.encodeUrl = false;
128
+ } else {
129
+ settings.encodeUrl = true;
130
+ }
131
+
132
+ const timeout = httpSettings?.timeout;
133
+ if (isNumber(timeout)) {
134
+ settings.timeout = timeout;
135
+ } else {
136
+ settings.timeout = 0;
137
+ }
138
+
139
+ if (httpSettings?.followRedirects === true) {
140
+ settings.followRedirects = true;
141
+ } else if (httpSettings?.followRedirects === false) {
142
+ settings.followRedirects = false;
143
+ } else {
144
+ settings.followRedirects = true;
145
+ }
146
+
147
+ const maxRedirects = httpSettings?.maxRedirects;
148
+ if (isNumber(maxRedirects)) {
149
+ settings.maxRedirects = maxRedirects;
150
+ } else {
151
+ settings.maxRedirects = 5;
152
+ }
153
+
154
+ ocRequest.settings = settings;
155
+
156
+ // docs
157
+ if (isNonEmptyString(brunoRequest.docs)) {
158
+ ocRequest.docs = brunoRequest.docs;
159
+ }
160
+
161
+ return stringifyYml(ocRequest);
162
+ } catch (error) {
163
+ console.error('Error stringifying GraphQL request:', error);
164
+ throw error;
165
+ }
166
+ };
167
+
168
+ export default stringifyGraphQLRequest;
@@ -0,0 +1,137 @@
1
+ import type { Item as BrunoItem } from '@usebruno/schema-types/collection/item';
2
+ import type { KeyValue as BrunoKeyValue } from '@usebruno/schema-types/common/key-value';
3
+ import type { GrpcRequest as BrunoGrpcRequest } from '@usebruno/schema-types/requests/grpc';
4
+ import type { GrpcRequest, GrpcMetadata, GrpcMessage, GrpcRequestInfo, GrpcRequestDetails, GrpcRequestRuntime } from '@opencollection/types/requests/grpc';
5
+ import type { Auth } from '@opencollection/types/common/auth';
6
+ import type { Scripts } from '@opencollection/types/common/scripts';
7
+ import type { Variable } from '@opencollection/types/common/variables';
8
+ import type { Assertion } from '@opencollection/types/common/assertions';
9
+ import { stringifyYml } from '../utils';
10
+ import { isNonEmptyString } from '../../../utils';
11
+ import { toOpenCollectionAuth } from '../common/auth';
12
+ import { toOpenCollectionVariables } from '../common/variables';
13
+ import { toOpenCollectionScripts } from '../common/scripts';
14
+ import { toOpenCollectionAssertions } from '../common/assertions';
15
+
16
+ const stringifyGrpcRequest = (item: BrunoItem): string => {
17
+ try {
18
+ const ocRequest: GrpcRequest = {};
19
+ const brunoRequest = item.request as BrunoGrpcRequest;
20
+
21
+ // info block
22
+ const info: GrpcRequestInfo = {
23
+ name: isNonEmptyString(item.name) ? item.name : 'Untitled Request',
24
+ type: 'grpc'
25
+ };
26
+ if (item.seq) {
27
+ info.seq = item.seq;
28
+ }
29
+ if (item.tags?.length) {
30
+ info.tags = item.tags;
31
+ }
32
+ ocRequest.info = info;
33
+
34
+ // grpc block
35
+ const grpc: GrpcRequestDetails = {
36
+ url: isNonEmptyString(brunoRequest.url) ? brunoRequest.url : '',
37
+ method: isNonEmptyString(brunoRequest.method) ? brunoRequest.method : ''
38
+ };
39
+
40
+ // method type
41
+ if (brunoRequest.methodType) {
42
+ grpc.methodType = brunoRequest.methodType;
43
+ }
44
+
45
+ // proto file path
46
+ if (isNonEmptyString(brunoRequest.protoPath)) {
47
+ grpc.protoFilePath = brunoRequest.protoPath;
48
+ }
49
+
50
+ // metadata
51
+ if (brunoRequest.headers?.length) {
52
+ const metadata: GrpcMetadata[] = brunoRequest.headers.map((header: BrunoKeyValue) => {
53
+ const metadataItem: GrpcMetadata = {
54
+ name: header.name || '',
55
+ value: header.value || ''
56
+ };
57
+
58
+ if (header?.description?.trim().length) {
59
+ metadataItem.description = header.description;
60
+ }
61
+
62
+ if (header.enabled === false) {
63
+ metadataItem.disabled = true;
64
+ }
65
+
66
+ return metadataItem;
67
+ });
68
+
69
+ if (metadata.length) {
70
+ grpc.metadata = metadata;
71
+ }
72
+ }
73
+
74
+ // message
75
+ if (brunoRequest.body?.mode === 'grpc' && brunoRequest.body.grpc?.length) {
76
+ const messages = brunoRequest.body.grpc;
77
+
78
+ // todo: bruno app supports only one message for now
79
+ // update this when bruno app supports multiple messages
80
+ if (messages.length) {
81
+ const message: GrpcMessage = messages[0].content || '';
82
+ if (message.trim().length) {
83
+ grpc.message = message;
84
+ }
85
+ }
86
+ }
87
+
88
+ // auth
89
+ const auth: Auth | undefined = toOpenCollectionAuth(brunoRequest.auth);
90
+ if (auth) {
91
+ grpc.auth = auth;
92
+ }
93
+
94
+ ocRequest.grpc = grpc;
95
+
96
+ // runtime block
97
+ const runtime: GrpcRequestRuntime = {};
98
+ let hasRuntime = false;
99
+
100
+ // variables
101
+ const variables: Variable[] | undefined = toOpenCollectionVariables(brunoRequest.vars);
102
+ if (variables) {
103
+ runtime.variables = variables;
104
+ hasRuntime = true;
105
+ }
106
+
107
+ // scripts
108
+ const scripts: Scripts | undefined = toOpenCollectionScripts(brunoRequest);
109
+ if (scripts) {
110
+ runtime.scripts = scripts;
111
+ hasRuntime = true;
112
+ }
113
+
114
+ // assertions
115
+ const assertions: Assertion[] | undefined = toOpenCollectionAssertions(brunoRequest.assertions);
116
+ if (assertions) {
117
+ runtime.assertions = assertions;
118
+ hasRuntime = true;
119
+ }
120
+
121
+ if (hasRuntime) {
122
+ ocRequest.runtime = runtime;
123
+ }
124
+
125
+ // docs
126
+ if (isNonEmptyString(brunoRequest.docs)) {
127
+ ocRequest.docs = brunoRequest.docs;
128
+ }
129
+
130
+ return stringifyYml(ocRequest);
131
+ } catch (error) {
132
+ console.error('Error stringifying gRPC request:', error);
133
+ throw error;
134
+ }
135
+ };
136
+
137
+ export default stringifyGrpcRequest;
@@ -0,0 +1,221 @@
1
+ import type { Item as BrunoItem, HttpItemSettings as BrunoHttpItemSettings } from '@usebruno/schema-types/collection/item';
2
+ import type { HttpRequest as BrunoHttpRequest } from '@usebruno/schema-types/requests/http';
3
+ import type { HttpRequest, HttpRequestSettings, HttpRequestExample, HttpRequestInfo, HttpRequestDetails, HttpRequestRuntime, HttpRequestHeader } from '@opencollection/types/requests/http';
4
+ import type { Auth } from '@opencollection/types/common/auth';
5
+ import type { Scripts } from '@opencollection/types/common/scripts';
6
+ import type { Variable } from '@opencollection/types/common/variables';
7
+ import type { Assertion } from '@opencollection/types/common/assertions';
8
+ import type { Action } from '@opencollection/types/common/actions';
9
+ import type { HttpRequestParam, HttpRequestBody } from '@opencollection/types/requests/http';
10
+ import { stringifyYml } from '../utils';
11
+ import { toOpenCollectionAuth } from '../common/auth';
12
+ import { toOpenCollectionHttpHeaders, toOpenCollectionResponseHeaders } from '../common/headers';
13
+ import { toOpenCollectionParams } from '../common/params';
14
+ import { toOpenCollectionBody } from '../common/body';
15
+ import { toOpenCollectionVariables } from '../common/variables';
16
+ import { toOpenCollectionActions } from '../common/actions';
17
+ import { toOpenCollectionScripts } from '../common/scripts';
18
+ import { toOpenCollectionAssertions } from '../common/assertions';
19
+ import { isNumber, isNonEmptyString } from '../../../utils';
20
+
21
+ const stringifyHttpRequest = (item: BrunoItem): string => {
22
+ try {
23
+ const ocRequest: HttpRequest = {};
24
+ const brunoRequest = item.request as BrunoHttpRequest;
25
+
26
+ // info block
27
+ const info: HttpRequestInfo = {
28
+ name: isNonEmptyString(item.name) ? item.name : 'Untitled Request',
29
+ type: 'http'
30
+ };
31
+ if (item.seq) {
32
+ info.seq = item.seq;
33
+ }
34
+ if (item.tags?.length) {
35
+ info.tags = item.tags;
36
+ }
37
+ ocRequest.info = info;
38
+
39
+ // http block
40
+ const http: HttpRequestDetails = {
41
+ method: isNonEmptyString(brunoRequest.method) ? brunoRequest.method : 'GET',
42
+ url: isNonEmptyString(brunoRequest.url) ? brunoRequest.url : ''
43
+ };
44
+
45
+ // headers
46
+ const headers: HttpRequestHeader[] | undefined = toOpenCollectionHttpHeaders(brunoRequest.headers);
47
+ if (headers) {
48
+ http.headers = headers;
49
+ }
50
+
51
+ // params
52
+ const params: HttpRequestParam[] | undefined = toOpenCollectionParams(brunoRequest.params);
53
+ if (params) {
54
+ http.params = params;
55
+ }
56
+
57
+ // body
58
+ const body: HttpRequestBody | undefined = toOpenCollectionBody(brunoRequest.body);
59
+ if (body) {
60
+ http.body = body;
61
+ }
62
+
63
+ // auth
64
+ const auth: Auth | undefined = toOpenCollectionAuth(brunoRequest.auth);
65
+ if (auth) {
66
+ http.auth = auth;
67
+ }
68
+
69
+ ocRequest.http = http;
70
+
71
+ // runtime block
72
+ const runtime: HttpRequestRuntime = {};
73
+ let hasRuntime = false;
74
+
75
+ // variables
76
+ const variables: Variable[] | undefined = toOpenCollectionVariables(brunoRequest.vars);
77
+ if (variables) {
78
+ runtime.variables = variables;
79
+ hasRuntime = true;
80
+ }
81
+
82
+ // scripts
83
+ const scripts: Scripts | undefined = toOpenCollectionScripts(brunoRequest);
84
+ if (scripts) {
85
+ runtime.scripts = scripts;
86
+ hasRuntime = true;
87
+ }
88
+
89
+ // assertions
90
+ const assertions: Assertion[] | undefined = toOpenCollectionAssertions(brunoRequest.assertions);
91
+ if (assertions) {
92
+ runtime.assertions = assertions;
93
+ hasRuntime = true;
94
+ }
95
+
96
+ // actions (from post-response variables)
97
+ const resVars = brunoRequest.vars?.res;
98
+ const actions: Action[] | undefined = toOpenCollectionActions(resVars);
99
+ if (actions) {
100
+ runtime.actions = actions;
101
+ hasRuntime = true;
102
+ }
103
+
104
+ if (hasRuntime) {
105
+ ocRequest.runtime = runtime;
106
+ }
107
+
108
+ // settings
109
+ const httpSettings = item.settings as BrunoHttpItemSettings | undefined;
110
+ const settings: HttpRequestSettings = {};
111
+ if (httpSettings?.encodeUrl === true) {
112
+ settings.encodeUrl = true;
113
+ } else if (httpSettings?.encodeUrl === false) {
114
+ settings.encodeUrl = false;
115
+ } else {
116
+ settings.encodeUrl = true;
117
+ }
118
+
119
+ const timeout = httpSettings?.timeout;
120
+ if (isNumber(timeout)) {
121
+ settings.timeout = timeout;
122
+ } else {
123
+ settings.timeout = 0;
124
+ }
125
+
126
+ if (httpSettings?.followRedirects === true) {
127
+ settings.followRedirects = true;
128
+ } else if (httpSettings?.followRedirects === false) {
129
+ settings.followRedirects = false;
130
+ } else {
131
+ settings.followRedirects = true;
132
+ }
133
+
134
+ const maxRedirects = httpSettings?.maxRedirects;
135
+ if (isNumber(maxRedirects)) {
136
+ settings.maxRedirects = maxRedirects;
137
+ } else {
138
+ settings.maxRedirects = 5;
139
+ }
140
+
141
+ ocRequest.settings = settings;
142
+
143
+ // examples
144
+ if (item.examples?.length) {
145
+ const examples: HttpRequestExample[] = item.examples.map((example) => {
146
+ const ocExample: HttpRequestExample = {};
147
+ ocExample.name = example?.name || 'Untitled Example';
148
+
149
+ if (isNonEmptyString(example.description)) {
150
+ ocExample.description = example.description;
151
+ }
152
+
153
+ if (example.request) {
154
+ ocExample.request = {};
155
+ ocExample.request.url = example.request.url || '';
156
+ ocExample.request.method = example.request.method || 'GET';
157
+
158
+ const exampleHeaders = toOpenCollectionHttpHeaders(example.request.headers);
159
+ if (exampleHeaders) {
160
+ ocExample.request.headers = exampleHeaders;
161
+ }
162
+
163
+ const exampleParams = toOpenCollectionParams(example.request.params);
164
+ if (exampleParams) {
165
+ ocExample.request.params = exampleParams;
166
+ }
167
+
168
+ const exampleBody = toOpenCollectionBody(example.request.body);
169
+ if (exampleBody !== undefined) {
170
+ ocExample.request.body = exampleBody;
171
+ }
172
+ }
173
+
174
+ if (example.response) {
175
+ ocExample.response = {};
176
+
177
+ if (example.response.status !== undefined && example.response.status !== null && isNumber(example.response.status)) {
178
+ ocExample.response.status = Number(example.response.status);
179
+ }
180
+
181
+ if (isNonEmptyString(example.response.statusText)) {
182
+ ocExample.response.statusText = example.response.statusText;
183
+ }
184
+
185
+ const responseHeaders = toOpenCollectionResponseHeaders(example.response.headers);
186
+ if (responseHeaders) {
187
+ ocExample.response.headers = responseHeaders;
188
+ }
189
+
190
+ if (example.response.body && example.response.body.type && example.response.body.content !== undefined) {
191
+ const content = example.response.body.content;
192
+ const contentString = typeof content === 'string' ? content : JSON.stringify(content, null, 2);
193
+
194
+ ocExample.response.body = {
195
+ type: example.response.body.type as 'json' | 'text' | 'xml' | 'html' | 'binary',
196
+ data: contentString
197
+ };
198
+ }
199
+ }
200
+
201
+ return ocExample;
202
+ });
203
+
204
+ if (examples?.length) {
205
+ ocRequest.examples = examples;
206
+ }
207
+ }
208
+
209
+ // docs
210
+ if (isNonEmptyString(brunoRequest.docs)) {
211
+ ocRequest.docs = brunoRequest.docs;
212
+ }
213
+
214
+ return stringifyYml(ocRequest);
215
+ } catch (error) {
216
+ console.error('Error stringifying HTTP request:', error);
217
+ throw error;
218
+ }
219
+ };
220
+
221
+ export default stringifyHttpRequest;
@@ -0,0 +1,22 @@
1
+ import type { Item as BrunoItem } from '@usebruno/schema-types/collection/item';
2
+ import type { ScriptFile } from '@opencollection/types/collection/item';
3
+ import { stringifyYml } from '../utils';
4
+
5
+ const stringifyScript = (item: BrunoItem): string => {
6
+ try {
7
+ const ocScript: ScriptFile = {
8
+ type: 'script'
9
+ };
10
+
11
+ if (item.fileContent?.trim().length) {
12
+ ocScript.script = item.fileContent;
13
+ }
14
+
15
+ return stringifyYml(ocScript);
16
+ } catch (error) {
17
+ console.error('Error stringifying script:', error);
18
+ throw error;
19
+ }
20
+ };
21
+
22
+ export default stringifyScript;
@@ -0,0 +1,114 @@
1
+ import type { Item as BrunoItem } from '@usebruno/schema-types/collection/item';
2
+ import type { WebSocketRequest as BrunoWebSocketRequest } from '@usebruno/schema-types/requests/websocket';
3
+ import type { WebSocketRequest, WebSocketMessage, WebSocketRequestInfo, WebSocketRequestDetails, WebSocketRequestRuntime } from '@opencollection/types/requests/websocket';
4
+ import type { Auth } from '@opencollection/types/common/auth';
5
+ import type { Scripts } from '@opencollection/types/common/scripts';
6
+ import type { Variable } from '@opencollection/types/common/variables';
7
+ import type { HttpRequestHeader } from '@opencollection/types/requests/http';
8
+ import { stringifyYml } from '../utils';
9
+ import { isNonEmptyString } from '../../../utils';
10
+ import { toOpenCollectionAuth } from '../common/auth';
11
+ import { toOpenCollectionHttpHeaders } from '../common/headers';
12
+ import { toOpenCollectionVariables } from '../common/variables';
13
+ import { toOpenCollectionScripts } from '../common/scripts';
14
+
15
+ const stringifyWebsocketRequest = (item: BrunoItem): string => {
16
+ try {
17
+ const ocRequest: WebSocketRequest = {};
18
+ const brunoRequest = item.request as BrunoWebSocketRequest;
19
+
20
+ // info block
21
+ const info: WebSocketRequestInfo = {
22
+ name: isNonEmptyString(item.name) ? item.name : 'Untitled Request',
23
+ type: 'websocket'
24
+ };
25
+ if (item.seq) {
26
+ info.seq = item.seq;
27
+ }
28
+ if (item.tags?.length) {
29
+ info.tags = item.tags;
30
+ }
31
+ ocRequest.info = info;
32
+
33
+ // websocket block
34
+ const websocket: WebSocketRequestDetails = {
35
+ url: isNonEmptyString(brunoRequest.url) ? brunoRequest.url : ''
36
+ };
37
+
38
+ // headers
39
+ const headers: HttpRequestHeader[] | undefined = toOpenCollectionHttpHeaders(brunoRequest.headers);
40
+ if (headers) {
41
+ websocket.headers = headers;
42
+ }
43
+
44
+ // message
45
+ if (brunoRequest.body?.mode === 'ws' && brunoRequest.body.ws?.length) {
46
+ const messages = brunoRequest.body.ws;
47
+
48
+ // todo: bruno app supports only one message for now
49
+ // update this when bruno app supports multiple messages
50
+ if (messages.length) {
51
+ const msg = messages[0];
52
+ const message: WebSocketMessage = {
53
+ type: (msg.type as 'text' | 'json' | 'xml' | 'binary') || 'text',
54
+ data: msg.content || ''
55
+ };
56
+ if (message.data.trim().length) {
57
+ websocket.message = message;
58
+ }
59
+ }
60
+ }
61
+
62
+ // auth
63
+ const auth: Auth | undefined = toOpenCollectionAuth(brunoRequest.auth);
64
+ if (auth) {
65
+ websocket.auth = auth;
66
+ }
67
+
68
+ ocRequest.websocket = websocket;
69
+
70
+ // runtime block
71
+ const runtime: WebSocketRequestRuntime = {};
72
+ let hasRuntime = false;
73
+
74
+ // variables
75
+ const variables: Variable[] | undefined = toOpenCollectionVariables(brunoRequest.vars);
76
+ if (variables) {
77
+ runtime.variables = variables;
78
+ hasRuntime = true;
79
+ }
80
+
81
+ // scripts
82
+ const scripts: Scripts | undefined = toOpenCollectionScripts(brunoRequest);
83
+ if (scripts) {
84
+ runtime.scripts = scripts;
85
+ hasRuntime = true;
86
+ }
87
+
88
+ if (hasRuntime) {
89
+ ocRequest.runtime = runtime;
90
+ }
91
+
92
+ // settings
93
+ const wsSettings = item.settings as Record<string, number | string | undefined> | undefined;
94
+ if (wsSettings) {
95
+ ocRequest.settings = {};
96
+ const timeout = Number(wsSettings.timeout);
97
+ ocRequest.settings.timeout = !isNaN(timeout) ? timeout : 0;
98
+ const keepAliveInterval = Number(wsSettings.keepAliveInterval);
99
+ ocRequest.settings.keepAliveInterval = !isNaN(keepAliveInterval) ? keepAliveInterval : 0;
100
+ }
101
+
102
+ // docs
103
+ if (isNonEmptyString(brunoRequest.docs)) {
104
+ ocRequest.docs = brunoRequest.docs;
105
+ }
106
+
107
+ return stringifyYml(ocRequest);
108
+ } catch (error) {
109
+ console.error('Error stringifying WebSocket request:', error);
110
+ throw error;
111
+ }
112
+ };
113
+
114
+ export default stringifyWebsocketRequest;