@adobe/generator-aio-app
Version:
Adobe I/O application yeoman code generator
72 lines (63 loc) • 2.74 kB
JavaScript
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const path = require('path')
const ActionGenerator = require('../../../lib/ActionGenerator')
const { commonDependencyVersions } = require('../../../lib/constants')
class GenericGenerator extends ActionGenerator {
constructor (args, opts) {
super(args, opts)
this.props = {
description: 'This is a sample action showcasing how to access an external API',
// eslint-disable-next-line quotes
requiredParams: `[/* add required params */]`,
// eslint-disable-next-line quotes
requiredHeaders: `['Authorization']`,
// eslint-disable-next-line quotes
importCode: `const fetch = require('node-fetch')
const { Core } = require('@adobe/aio-sdk')`,
responseCode: `// replace this with the api you want to access
const apiEndpoint = 'https://adobeioruntime.net/api/v1/api-docs'
// fetch content from external api endpoint
const res = await fetch(apiEndpoint)
if (!res.ok) {
throw new Error('request to ' + apiEndpoint + ' failed with status code ' + res.status)
}
const content = await res.json()
logger.debug('fetch content = ' + JSON.stringify(content, null, 2))
const response = {
statusCode: 200,
body: content
}`
}
}
async prompting () {
this.props.actionName = await this.promptForActionName('showcases how to access an external API', 'generic')
}
writing () {
this.sourceRoot(path.join(__dirname, '.'))
this.addAction(this.props.actionName, '../../common-templates/stub-action.js', {
testFile: './templates/fetchExample.test.js',
sharedLibFile: '../../common-templates/utils.js',
sharedLibTestFile: '../../common-templates/utils.test.js',
e2eTestFile: '../../common-templates/stub-action.e2e.js',
tplContext: this.props,
dependencies: {
'@adobe/aio-sdk': commonDependencyVersions['@adobe/aio-sdk'],
'node-fetch': '^2.6.0'
},
actionManifestConfig: {
inputs: { LOG_LEVEL: 'debug' },
annotations: { final: true } // makes sure loglevel cannot be overwritten by request param
}
})
}
}
module.exports = GenericGenerator