@smartlyio/oats-playwright-adapter 7.8.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.
- package/LICENSE +14 -0
- package/README.md +17 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +153 -0
- package/dist/index.js.map +1 -0
- package/dist/src/assert.d.ts +1 -0
- package/dist/src/assert.js +7 -0
- package/dist/src/assert.js.map +1 -0
- package/index.ts +197 -0
- package/package.json +49 -0
- package/src/assert.ts +3 -0
- package/tsconfig.test.json +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Copyright 2019 Smartly.io
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
4
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
5
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
6
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
9
|
+
Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
12
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
13
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
14
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Playwright client adapter for smartlyio/oats
|
|
2
|
+
|
|
3
|
+
See [smartlyio/oats](https://github.com/smartlyio/oats)
|
|
4
|
+
|
|
5
|
+
Bind generated client definitions to Playwright's `APIRequestContext` (for example the `request` fixture in `@playwright/test`):
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { test } from '@playwright/test';
|
|
9
|
+
import * as adapter from '@smartlyio/oats-playwright-adapter';
|
|
10
|
+
|
|
11
|
+
test('calls api', async ({ request }) => {
|
|
12
|
+
const api = client(spec => adapter.create(request)(spec));
|
|
13
|
+
await api.get();
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can also create a standalone context with `playwright.request.newContext()`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as runtime from '@smartlyio/oats-runtime';
|
|
2
|
+
import type { APIRequestContext } from 'playwright';
|
|
3
|
+
export interface PlaywrightAdapterOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Whether to throw when response status is not 2xx or 3xx.
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
failOnStatusCode?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @returns oats runtime client adapter backed by Playwright {@link APIRequestContext}.
|
|
12
|
+
*/
|
|
13
|
+
export declare function create(request: APIRequestContext, { failOnStatusCode }?: PlaywrightAdapterOptions): runtime.client.ClientAdapter;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.create = create;
|
|
4
|
+
// eslint-disable-next-line import/no-nodejs-modules -- needed for multipart file uploads
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const runtime = require("@smartlyio/oats-runtime");
|
|
7
|
+
const assert_1 = require("./src/assert");
|
|
8
|
+
function formBinaryOptions(options) {
|
|
9
|
+
if (options == null) {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
if (typeof options === 'string') {
|
|
13
|
+
return { filename: options };
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
filename: options.filename,
|
|
17
|
+
contentType: options.contentType
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function toMultipartValue(binary, options) {
|
|
21
|
+
if (binary instanceof runtime.make.FormBinary) {
|
|
22
|
+
return toMultipartValue(binary.binary, binary.options);
|
|
23
|
+
}
|
|
24
|
+
if (binary instanceof runtime.make.File) {
|
|
25
|
+
return fs.createReadStream(binary.path);
|
|
26
|
+
}
|
|
27
|
+
if (Buffer.isBuffer(binary)) {
|
|
28
|
+
const { filename, contentType } = formBinaryOptions(options);
|
|
29
|
+
return {
|
|
30
|
+
name: filename ?? 'file',
|
|
31
|
+
mimeType: contentType ?? 'application/octet-stream',
|
|
32
|
+
buffer: binary
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return (0, assert_1.fail)('unknown binary type for playwright multipart body');
|
|
36
|
+
}
|
|
37
|
+
function toRequestBody(data) {
|
|
38
|
+
if (data == null) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (data.contentType === 'text/plain') {
|
|
42
|
+
return { data: data.value };
|
|
43
|
+
}
|
|
44
|
+
if (data.contentType === 'application/json') {
|
|
45
|
+
return { data: JSON.stringify(data.value) };
|
|
46
|
+
}
|
|
47
|
+
if (data.contentType === 'application/x-www-form-urlencoded') {
|
|
48
|
+
return { form: data.value };
|
|
49
|
+
}
|
|
50
|
+
if (data.contentType === 'multipart/form-data') {
|
|
51
|
+
const multipart = {};
|
|
52
|
+
Object.keys(data.value).forEach(key => {
|
|
53
|
+
const element = data.value[key];
|
|
54
|
+
if (element instanceof runtime.make.FormBinary) {
|
|
55
|
+
multipart[key] = toMultipartValue(element.binary, element.options);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
multipart[key] = element;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return { multipart };
|
|
62
|
+
}
|
|
63
|
+
(0, assert_1.fail)('unknown content type for playwright client ' + data.contentType);
|
|
64
|
+
}
|
|
65
|
+
function getContentType(response) {
|
|
66
|
+
const type = response.headers()['content-type'];
|
|
67
|
+
if (!type || type === runtime.noContentContentType) {
|
|
68
|
+
return runtime.noContentContentType;
|
|
69
|
+
}
|
|
70
|
+
if (response.status() === 204) {
|
|
71
|
+
return 'text/plain';
|
|
72
|
+
}
|
|
73
|
+
return type.split(';')[0].trim();
|
|
74
|
+
}
|
|
75
|
+
async function getResponseData(contentType, response) {
|
|
76
|
+
if (contentType === runtime.noContentContentType) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
if (response.status() === 204) {
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
if (contentType.includes('application/json')) {
|
|
83
|
+
return response.json();
|
|
84
|
+
}
|
|
85
|
+
return response.text();
|
|
86
|
+
}
|
|
87
|
+
function abortError() {
|
|
88
|
+
const error = new Error('This operation was aborted');
|
|
89
|
+
error.name = 'AbortError';
|
|
90
|
+
return error;
|
|
91
|
+
}
|
|
92
|
+
function throwIfAborted(signal) {
|
|
93
|
+
if (signal?.aborted) {
|
|
94
|
+
throw abortError();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function abortable(promise, signal) {
|
|
98
|
+
if (!signal) {
|
|
99
|
+
return promise;
|
|
100
|
+
}
|
|
101
|
+
throwIfAborted(signal);
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
const onAbort = () => {
|
|
104
|
+
reject(abortError());
|
|
105
|
+
};
|
|
106
|
+
signal.addEventListener('abort', onAbort);
|
|
107
|
+
promise.then(value => {
|
|
108
|
+
signal.removeEventListener('abort', onAbort);
|
|
109
|
+
resolve(value);
|
|
110
|
+
}, error => {
|
|
111
|
+
signal.removeEventListener('abort', onAbort);
|
|
112
|
+
reject(error);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* @returns oats runtime client adapter backed by Playwright {@link APIRequestContext}.
|
|
118
|
+
*/
|
|
119
|
+
function create(request, { failOnStatusCode = false } = {}) {
|
|
120
|
+
return async (arg) => {
|
|
121
|
+
if (arg.servers.length !== 1) {
|
|
122
|
+
return (0, assert_1.fail)('cannot decide which server to use from ' + arg.servers.join(', '));
|
|
123
|
+
}
|
|
124
|
+
const server = arg.servers[0];
|
|
125
|
+
const requestBody = toRequestBody(arg.body);
|
|
126
|
+
const url = new URL(server + arg.path);
|
|
127
|
+
const requestContentType = arg.body?.contentType;
|
|
128
|
+
Object.entries(arg.query ?? {})
|
|
129
|
+
.filter(([, value]) => !!value)
|
|
130
|
+
.forEach(([key, value]) => Array.isArray(value)
|
|
131
|
+
? value.forEach(v => url.searchParams.append(key, `${v}`))
|
|
132
|
+
: url.searchParams.append(key, `${value}`));
|
|
133
|
+
const response = await abortable(request.fetch(url.toString(), {
|
|
134
|
+
method: arg.method.toUpperCase(),
|
|
135
|
+
headers: {
|
|
136
|
+
...(requestContentType ? { 'content-type': requestContentType } : {}),
|
|
137
|
+
...arg.headers
|
|
138
|
+
},
|
|
139
|
+
failOnStatusCode,
|
|
140
|
+
...requestBody
|
|
141
|
+
}), arg.signal);
|
|
142
|
+
const contentType = getContentType(response);
|
|
143
|
+
return {
|
|
144
|
+
status: response.status(),
|
|
145
|
+
value: {
|
|
146
|
+
contentType,
|
|
147
|
+
value: await getResponseData(contentType, response)
|
|
148
|
+
},
|
|
149
|
+
headers: response.headers()
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;AAuJA,wBA6CC;AApMD,yFAAyF;AACzF,yBAAyB;AACzB,mDAAmD;AAEnD,yCAAoC;AASpC,SAAS,iBAAiB,CAAC,OAA2C;IAIpE,IAAI,OAAO,IAAI,IAAI,EAA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AAY,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;YAC1D,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,KAAK,EAAE,CAAC,CAC7C,CAAC;QAEJ,MAAM,QAAQ,GAAG,MAAM,SAAS,CAC9B,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE;YAC5B,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,WAAW,EAAE;YAChC,OAAO,EAAE;gBACP,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACrE,GAAG,GAAG,CAAC,OAAO;aACf;YACD,gBAAgB;YAChB,GAAG,WAAW;SACf,CAAC,EACF,GAAG,CAAC,MAAM,CACX,CAAC;QAEF,MAAM,WAAW,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;QAE7C,OAAO;YACL,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE;YACzB,KAAK,EAAE;gBACL,WAAW;gBACX,KAAK,EAAE,MAAM,eAAe,CAAC,WAAW,EAAE,QAAQ,CAAC;aACpD;YACD,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE;SAC5B,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fail(msg: string): never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../../src/assert.ts"],"names":[],"mappings":";;AAAA,oBAEC;AAFD,SAAgB,IAAI,CAAC,GAAW;IAC9B,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;AACvB,CAAC"}
|
package/index.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// eslint-disable-next-line import/no-nodejs-modules -- needed for multipart file uploads
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as runtime from '@smartlyio/oats-runtime';
|
|
4
|
+
import type { APIRequestContext, APIResponse } from 'playwright';
|
|
5
|
+
import { fail } from './src/assert';
|
|
6
|
+
|
|
7
|
+
type PlaywrightFetchOptions = NonNullable<Parameters<APIRequestContext['fetch']>[1]>;
|
|
8
|
+
type PlaywrightFetchBody = Pick<PlaywrightFetchOptions, 'data' | 'form' | 'multipart'>;
|
|
9
|
+
type PlaywrightMultipart = Exclude<
|
|
10
|
+
PlaywrightFetchBody['multipart'] & Record<string, unknown>,
|
|
11
|
+
FormData
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
function formBinaryOptions(options: runtime.make.FormBinary['options']): {
|
|
15
|
+
filename?: string;
|
|
16
|
+
contentType?: string;
|
|
17
|
+
} {
|
|
18
|
+
if (options == null) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
if (typeof options === 'string') {
|
|
22
|
+
return { filename: options };
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
filename: options.filename,
|
|
26
|
+
contentType: options.contentType
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toMultipartValue(
|
|
31
|
+
binary: runtime.make.Binary,
|
|
32
|
+
options: runtime.make.FormBinary['options']
|
|
33
|
+
): PlaywrightMultipart[keyof PlaywrightMultipart] {
|
|
34
|
+
if (binary instanceof runtime.make.FormBinary) {
|
|
35
|
+
return toMultipartValue(binary.binary, binary.options);
|
|
36
|
+
}
|
|
37
|
+
if (binary instanceof runtime.make.File) {
|
|
38
|
+
return fs.createReadStream(binary.path);
|
|
39
|
+
}
|
|
40
|
+
if (Buffer.isBuffer(binary)) {
|
|
41
|
+
const { filename, contentType } = formBinaryOptions(options);
|
|
42
|
+
return {
|
|
43
|
+
name: filename ?? 'file',
|
|
44
|
+
mimeType: contentType ?? 'application/octet-stream',
|
|
45
|
+
buffer: binary
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return fail('unknown binary type for playwright multipart body');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function toRequestBody(
|
|
52
|
+
data: runtime.server.RequestBody<any> | undefined
|
|
53
|
+
): PlaywrightFetchBody | undefined {
|
|
54
|
+
if (data == null) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
if (data.contentType === 'text/plain') {
|
|
58
|
+
return { data: data.value };
|
|
59
|
+
}
|
|
60
|
+
if (data.contentType === 'application/json') {
|
|
61
|
+
return { data: JSON.stringify(data.value) };
|
|
62
|
+
}
|
|
63
|
+
if (data.contentType === 'application/x-www-form-urlencoded') {
|
|
64
|
+
return { form: data.value };
|
|
65
|
+
}
|
|
66
|
+
if (data.contentType === 'multipart/form-data') {
|
|
67
|
+
const multipart: PlaywrightMultipart = {};
|
|
68
|
+
Object.keys(data.value).forEach(key => {
|
|
69
|
+
const element = data.value[key];
|
|
70
|
+
if (element instanceof runtime.make.FormBinary) {
|
|
71
|
+
multipart[key] = toMultipartValue(element.binary, element.options);
|
|
72
|
+
} else {
|
|
73
|
+
multipart[key] = element;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return { multipart };
|
|
77
|
+
}
|
|
78
|
+
fail('unknown content type for playwright client ' + data.contentType);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getContentType(response: APIResponse) {
|
|
82
|
+
const type = response.headers()['content-type'];
|
|
83
|
+
if (!type || type === runtime.noContentContentType) {
|
|
84
|
+
return runtime.noContentContentType;
|
|
85
|
+
}
|
|
86
|
+
if (response.status() === 204) {
|
|
87
|
+
return 'text/plain';
|
|
88
|
+
}
|
|
89
|
+
return type.split(';')[0].trim();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function getResponseData(contentType: string, response: APIResponse) {
|
|
93
|
+
if (contentType === runtime.noContentContentType) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (response.status() === 204) {
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
if (contentType.includes('application/json')) {
|
|
100
|
+
return response.json();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return response.text();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function abortError() {
|
|
107
|
+
const error = new Error('This operation was aborted');
|
|
108
|
+
error.name = 'AbortError';
|
|
109
|
+
return error;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function throwIfAborted(signal: AbortSignal | undefined) {
|
|
113
|
+
if (signal?.aborted) {
|
|
114
|
+
throw abortError();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function abortable<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T> {
|
|
119
|
+
if (!signal) {
|
|
120
|
+
return promise;
|
|
121
|
+
}
|
|
122
|
+
throwIfAborted(signal);
|
|
123
|
+
return new Promise((resolve, reject) => {
|
|
124
|
+
const onAbort = () => {
|
|
125
|
+
reject(abortError());
|
|
126
|
+
};
|
|
127
|
+
signal.addEventListener('abort', onAbort);
|
|
128
|
+
promise.then(
|
|
129
|
+
value => {
|
|
130
|
+
signal.removeEventListener('abort', onAbort);
|
|
131
|
+
resolve(value);
|
|
132
|
+
},
|
|
133
|
+
error => {
|
|
134
|
+
signal.removeEventListener('abort', onAbort);
|
|
135
|
+
reject(error);
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface PlaywrightAdapterOptions {
|
|
142
|
+
/**
|
|
143
|
+
* Whether to throw when response status is not 2xx or 3xx.
|
|
144
|
+
* @default false
|
|
145
|
+
*/
|
|
146
|
+
failOnStatusCode?: boolean;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @returns oats runtime client adapter backed by Playwright {@link APIRequestContext}.
|
|
151
|
+
*/
|
|
152
|
+
export function create(
|
|
153
|
+
request: APIRequestContext,
|
|
154
|
+
{ failOnStatusCode = false }: PlaywrightAdapterOptions = {}
|
|
155
|
+
): runtime.client.ClientAdapter {
|
|
156
|
+
return async arg => {
|
|
157
|
+
if (arg.servers.length !== 1) {
|
|
158
|
+
return fail('cannot decide which server to use from ' + arg.servers.join(', '));
|
|
159
|
+
}
|
|
160
|
+
const server = arg.servers[0];
|
|
161
|
+
const requestBody = toRequestBody(arg.body);
|
|
162
|
+
const url = new URL(server + arg.path);
|
|
163
|
+
const requestContentType = arg.body?.contentType;
|
|
164
|
+
|
|
165
|
+
Object.entries(arg.query ?? {})
|
|
166
|
+
.filter(([, value]) => !!value)
|
|
167
|
+
.forEach(([key, value]) =>
|
|
168
|
+
Array.isArray(value)
|
|
169
|
+
? value.forEach(v => url.searchParams.append(key, `${v}`))
|
|
170
|
+
: url.searchParams.append(key, `${value}`)
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const response = await abortable(
|
|
174
|
+
request.fetch(url.toString(), {
|
|
175
|
+
method: arg.method.toUpperCase(),
|
|
176
|
+
headers: {
|
|
177
|
+
...(requestContentType ? { 'content-type': requestContentType } : {}),
|
|
178
|
+
...arg.headers
|
|
179
|
+
},
|
|
180
|
+
failOnStatusCode,
|
|
181
|
+
...requestBody
|
|
182
|
+
}),
|
|
183
|
+
arg.signal
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const contentType = getContentType(response);
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
status: response.status(),
|
|
190
|
+
value: {
|
|
191
|
+
contentType,
|
|
192
|
+
value: await getResponseData(contentType, response)
|
|
193
|
+
},
|
|
194
|
+
headers: response.headers()
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@smartlyio/oats-playwright-adapter",
|
|
3
|
+
"version": "7.8.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Playwright adapter for Oats a Openapi3 based generator for typescript aware servers and clients",
|
|
6
|
+
"private": false,
|
|
7
|
+
"author": "Smartly.io",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/smartlyio/oats.git"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@smartlyio/oats-runtime": "^6.3.0 || ^7.0.0",
|
|
16
|
+
"playwright": "^1.40.0",
|
|
17
|
+
"typescript": "^4.8.0 || ^5.0.0"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"oats",
|
|
21
|
+
"openapi3",
|
|
22
|
+
"rest",
|
|
23
|
+
"generator",
|
|
24
|
+
"typescript",
|
|
25
|
+
"server",
|
|
26
|
+
"client",
|
|
27
|
+
"playwright"
|
|
28
|
+
],
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "24.12.4",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "5.62.0",
|
|
32
|
+
"@typescript-eslint/parser": "5.62.0",
|
|
33
|
+
"eslint": "8.57.1",
|
|
34
|
+
"eslint-config-prettier": "8.10.2",
|
|
35
|
+
"eslint-plugin-import": "2.27.5",
|
|
36
|
+
"eslint-plugin-jest": "27.9.0",
|
|
37
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
38
|
+
"playwright": "1.58.0",
|
|
39
|
+
"prettier": "2.8.8",
|
|
40
|
+
"typescript": "5.9.2",
|
|
41
|
+
"@smartlyio/oats-runtime": "^7.8.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"clean": "rm -rf ./dist && rm -f ./tmp/*.ts",
|
|
45
|
+
"build": "pnpm clean && pnpm tsc",
|
|
46
|
+
"lint": "eslint --max-warnings=0 --ext .ts .",
|
|
47
|
+
"lint:fix": "eslint --max-warnings=0 --ext .ts . --fix"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/assert.ts
ADDED