diff --git a/craft-functions/fetch-weather-api-write-spreadsheet/README.md b/craft-functions/fetch-weather-api-write-spreadsheet/README.md index 2f36792..c190c95 100644 --- a/craft-functions/fetch-weather-api-write-spreadsheet/README.md +++ b/craft-functions/fetch-weather-api-write-spreadsheet/README.md @@ -14,3 +14,7 @@ Weather API から47都道府県ごとの天気情報を取得し、 Googleス ## category 外部連携,マーケティング,SERVER_SIDE_ACTION + +## functionType + +http \ No newline at end of file diff --git a/craft-functions/fetch-weather-api-write-spreadsheet/index.js b/craft-functions/fetch-weather-api-write-spreadsheet/index.js index ff9b13b..e3f66c0 100644 --- a/craft-functions/fetch-weather-api-write-spreadsheet/index.js +++ b/craft-functions/fetch-weather-api-write-spreadsheet/index.js @@ -43,7 +43,7 @@ const LOCATIONS = [ { prefecture: '広島県', location: 'hiroshima', lat: '34.2348', lon: '132.2735' }, { prefecture: '山口県', location: 'yamaguchi', lat: '34.1109', lon: '131.2817' }, { prefecture: '徳島県', location: 'tokushima', lat: '34.0357', lon: '134.3334' }, - { prefecture: '香川県', location: 'kagawa', lat: '34.2024', lon: '134.0236' }, + { prefecture: '香川県', location: 'kagawa', lat: '34.2024', lon: '134.0236' }, { prefecture: '愛媛県', location: 'ehime', lat: '33.5030', lon: '132.4558' }, { prefecture: '高知県', location: 'kochi', lat: '33.3335', lon: '133.3152' }, { prefecture: '福岡県', location: 'fukuoka', lat: '33.3623', lon: '130.2505' }, @@ -92,9 +92,19 @@ async function fetchData({ location, rows, logger }) { } export default async function (data, { MODULES }) { + const { req, res } = data; const { initLogger, secret } = MODULES; const logger = initLogger({ logLevel: LOG_LEVEL }); + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); + + if (req.method === 'OPTIONS') { + res.status(204).end(); + return; + } + // GoogleサービスアカウントのJSONキーをシークレットから取得 const secrets = await secret.get({ keys: [SERVICE_ACCOUNT_KEY_SECRET] }); const jsonKeySecrets = secrets[SERVICE_ACCOUNT_KEY_SECRET]; @@ -115,7 +125,9 @@ export default async function (data, { MODULES }) { await Promise.all(LOCATIONS.map(location => fetchData({ location, rows, logger }))); await updateSsValues(sheets, `${SHEET_NAME}!1:${LOCATIONS.length + 1}`, rows); logger.debug('Data fetch completed:', rows); + res.status(200).json({ message: 'Success' }); } catch (error) { logger.debug('Data fetch Failed:', error); + res.status(500).json({ message: 'Data fetch Failed' }); } -} +} \ No newline at end of file