mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
|
// cfc 入口。
|
||
|
exports.handler = (event, context, callback) => {
|
||
|
try {
|
||
|
const entry = require('./mock/index');
|
||
|
entry(mockRequest(event, context), mockResponse(event, context, callback));
|
||
|
} catch (e) {
|
||
|
callback(e.stack);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
function mockRequest(event, context) {
|
||
|
let body = {};
|
||
|
|
||
|
if (
|
||
|
event.body &&
|
||
|
typeof event.body === 'string' &&
|
||
|
/^application\/json/i.test(event.headers['Content-Type'])
|
||
|
) {
|
||
|
body = JSON.parse(event.body);
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
query: event.queryStringParameters || {},
|
||
|
method: 'GET',
|
||
|
body: body,
|
||
|
url: `/api/${event.pathParameters.subpath}`,
|
||
|
originalUrl: '',
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function mockResponse(event, context, callback) {
|
||
|
return {
|
||
|
json(json) {
|
||
|
callback(null, {
|
||
|
statusCode: 200,
|
||
|
headers: createHeaders(event.headers),
|
||
|
body: JSON.stringify(json),
|
||
|
});
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function createHeaders(headers) {
|
||
|
let referer = '';
|
||
|
|
||
|
if (/^(https?\:\/\/[^:\/]+(?:\:\d+)?\/)/i.test(headers['Referer'])) {
|
||
|
referer = RegExp.$1.replace(/\/$/, '');
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
'Content-Type': 'Application/json',
|
||
|
'Access-Control-Allow-Headers': 'x-requested-with,content-type',
|
||
|
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS,HEAD',
|
||
|
'Access-Control-Allow-Credentials': 'true',
|
||
|
'Access-Control-Allow-Origin': referer ? `${referer}` : '*',
|
||
|
};
|
||
|
}
|