Files
back_end_oudelaa/scripts/build-marketplace-postman.js

86 lines
3.8 KiB
JavaScript

// Build saved Postman examples from captured integration-test responses.
const fs = require('fs');
const captures = JSON.parse(fs.readFileSync('postman/marketplace-response-examples.json', 'utf8'));
const example = (name) => captures.examples.find((entry) => entry.name === name);
const ids = [
[example('Product details').response.id, '{{listingId}}'],
[example('Shop details').response.shopId, '{{shopId}}'],
[example('Set cart item quantity').response.items[0].id, '{{cartItemId}}'],
[example('Create order').response.id, '{{orderId}}'],
];
const template = (value) =>
ids.reduce((text, [id, variable]) => text.replaceAll(id, variable), value);
const collection = {
info: {
name: 'Oudelaa Marketplace — Flutter',
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json',
description: `${captures.source}\nCaptured at ${captures.capturedAt}. See MARKETPLACE-FLUTTER.md. Set baseUrl, accessToken, shopId and listingId. Saved examples use synthetic IDs. Only cash_on_delivery is supported. Online payments are deferred. One shop/currency per checkout. Page limits must be integers; home section limits 1..20, listing/repair limits 1..200. Search q maximum 120 characters. Home onlyActive=false includes active and inactive rows.`,
},
auth: { type: 'bearer', bearer: [{ key: 'token', value: '{{accessToken}}', type: 'string' }] },
variable: ['baseUrl', 'accessToken', 'shopId', 'listingId', 'cartItemId', 'orderId'].map(
(key) => ({
key,
value: key === 'baseUrl' ? 'http://localhost:4000/api/v1' : '',
type: 'string',
}),
),
item: captures.examples.map((entry) => {
const path = template(entry.path);
const publicRead = entry.method === 'GET' && /\/marketplace\/(home|listings|shops|instruments|repair-shops)/.test(path);
const req = {
method: entry.method,
header: entry.request ? [{ key: 'Content-Type', value: 'application/json' }] : [],
url: `{{baseUrl}}${path}`,
description:
entry.name === 'Set cart item quantity'
? 'Sets the absolute quantity, including for an existing listing. Does not increment on retries.'
: 'See the saved response example and MARKETPLACE-FLUTTER.md.',
...(publicRead ? { auth: { type: 'noauth' } } : {}),
...(entry.request
? {
body: {
mode: 'raw',
raw: template(JSON.stringify(entry.request, null, 2)),
options: { raw: { language: 'json' } },
},
}
: {}),
};
const tests = [
`pm.test('Expected HTTP status', () => pm.response.to.have.status(${entry.status}));`,
'const json = pm.response.json();',
'const body = json.data ?? json;',
];
if (entry.name === 'Set cart item quantity')
tests.push(
'if (body.items?.length) pm.collectionVariables.set("cartItemId", body.items[0].id);',
);
if (entry.name === 'Create order')
tests.push('if (body.id) pm.collectionVariables.set("orderId", body.id);');
return {
name: entry.name,
request: req,
...(tests.length > 0
? { event: [{ listen: 'test', script: { type: 'text/javascript', exec: tests } }] }
: {}),
response: [
{
name: `${entry.status} — captured integration response`,
originalRequest: req,
status: entry.status === 201 ? 'Created' : 'OK',
code: entry.status,
header: [{ key: 'Content-Type', value: 'application/json; charset=utf-8' }],
_postman_previewlanguage: 'json',
cookie: [],
body: JSON.stringify(entry.response, null, 2),
},
],
};
}),
};
fs.writeFileSync(
'postman/Oudelaa-Marketplace-Flutter.postman_collection.json',
JSON.stringify(collection, null, 2) + '\n',
);
console.log(`Wrote ${collection.item.length} requests with captured response examples.`);