Send to zulip

This commit is contained in:
Koper
2023-11-20 21:39:33 +07:00
parent 82f50817f8
commit ba40d28152
3609 changed files with 2311843 additions and 7 deletions

View File

@@ -0,0 +1,179 @@
{
"version": "2.0",
"metadata": {
"apiVersion": "2018-03-30",
"endpointPrefix": "foo",
"protocol": "rest-json",
"serviceId": "Foo",
"uid": "foo-2018-03-30"
},
"operations": {
"FancyOperation": {
"name" :"FancyOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"input": {
"shape": "FancyStructure"
}
},
"BarOperation": {
"name": "BarOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"input": {
"shape": "String"
},
"output": {
"shape": "String"
}
},
"EventStreamOnInputOperation": {
"name": "EventStreamOnInputOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"input": {
"shape": "String"
}
},
"EventStreamOnInputPayloadOperation": {
"name": "EventStreamOnInputPayloadOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"input": {
"shape": "String"
}
},
"EventStreamOnOutputOperation": {
"name": "EventStreamOnOutputOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"output": {
"shape": "String"
}
},
"EventStreamOnOutputPayloadOperation": {
"name": "EventStreamOnOutputPayloadOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"output": {
"shape": "String"
}
},
"BazOperation": {
"name": "BazOperation",
"http": {
"method": "GET",
"requestUri": "/"
},
"input": {
"shape": "String"
}
}
},
"shapes": {
"FancyStructure": {
"type": "structure",
"required": [],
"members": {
"Map": {
"shape": "MapOfList"
}
}
},
"ListOfString": {
"type": "list",
"member": {
"shape": "String"
}
},
"ListOfList": {
"type": "list",
"member": {
"shape": "ListOfString"
}
},
"MapOfString": {
"type": "map",
"key": {
"shape": "String"
},
"value": {
"shape": "String"
}
},
"MapOfList": {
"type": "map",
"key": {
"shape": "String"
},
"value": {
"shape": "ListOfString"
}
},
"String": {
"type": "string"
},
"BarOperationInput": {
"type": "structure",
"members": {
"String": {
"shape": "StringShape"
}
}
},
"BarOperationOutput": {
"type": "structure",
"members": {
"String": {
"shape": "StringShape"
}
}
},
"BazOperationInput": {
"type": "structure",
"members": {
"BazString": {
"shape": "BazStringShape",
"timestampFormat": "iso8601"
}
}
},
"EventStreamPayload": {
"type": "structure",
"members": {
"Payload": {
"shape": "EventStreamStructure"
},
"payload": "Payload"
}
},
"EventStreamStructure": {
"type": "structure",
"members": {
"String": {
"shape": "StringShape"
}
},
"eventstream": true
},
"BazStringShape": {
"type": "timestamp",
"timestampFormat": "rfc822"
},
"StringShape": {
"type": "string"
}
}
}

View File

@@ -0,0 +1,127 @@
{
"version": "2.0",
"metadata": {
"apiVersion": "2018-03-30",
"endpointPrefix": "foo",
"protocol": "rest-json",
"serviceId": "Foo",
"uid": "foo-2018-03-30"
},
"operations": {
"BarOperation": {
"name": "BarOperation",
"http": {
"method": "GET",
"requireUri": "/"
},
"input": {
"shape": "BarOperationInput"
},
"output": {
"shape": "BarOperationOutput"
}
},
"EventStreamOnInputOperation": {
"name": "EventStreamOnInputOperation",
"http": {
"method": "GET",
"requireUri": "/"
},
"input": {
"shape": "EventStreamStructure"
}
},
"EventStreamOnInputPayloadOperation": {
"name": "EventStreamOnInputPayloadOperation",
"http": {
"method": "GET",
"requireUri": "/"
},
"input": {
"shape": "EventStreamPayload"
}
},
"EventStreamOnOutputOperation": {
"name": "EventStreamOnOutputOperation",
"http": {
"method": "GET",
"requireUri": "/"
},
"output": {
"shape": "EventStreamStructure"
}
},
"EventStreamOnOutputPayloadOperation": {
"name": "EventStreamOnOutputPayloadOperation",
"http": {
"method": "GET",
"requireUri": "/"
},
"output": {
"shape": "EventStreamPayload"
}
},
"BazOperation": {
"name": "BazOperation",
"http": {
"method": "GET",
"requireUri": "/"
},
"input": {
"shape": "BazOperationInput"
}
}
},
"shapes": {
"BarOperationInput": {
"type": "structure",
"members": {
"String": {
"shape": "StringShape"
}
}
},
"BarOperationOutput": {
"type": "structure",
"members": {
"String": {
"shape": "StringShape"
}
}
},
"BazOperationInput": {
"type": "structure",
"members": {
"BazString": {
"shape": "BazStringShape",
"timestampFormat": "iso8601"
}
}
},
"EventStreamPayload": {
"type": "structure",
"members": {
"Payload": {
"shape": "EventStreamStructure"
},
"payload": "Payload"
}
},
"EventStreamStructure": {
"type": "structure",
"members": {
"String": {
"shape": "StringShape"
}
},
"eventstream": true
},
"BazStringShape": {
"type": "timestamp",
"timestampFormat": "rfc822"
},
"StringShape": {
"type": "string"
}
}
}

View File

@@ -0,0 +1,20 @@
function getOperationShapeNames(model) {
var operationShapeNames = [];
var operations = model.operations;
for (var operationName of Object.keys(operations)) {
var operation = operations[operationName];
if (operation.input && operation.input.shape) {
operationShapeNames.push(operation.input.shape);
}
if (operation.output && operation.output.shape) {
operationShapeNames.push(operation.output.shape);
}
}
return operationShapeNames;
};
module.exports = {
getOperationShapeNames: getOperationShapeNames
};

View File

@@ -0,0 +1,27 @@
var getOperationShapeNames = require('./get-operation-shape-names').getOperationShapeNames;
var visitRelatedShapeNames = require('./visit-related-shape-names').visitRelatedShapeNames;
function pruneShapes(model) {
// start by grabbing the input/output shapes on all operations
var operationShapeNames = getOperationShapeNames(model);
var shapeMap = model.shapes;
for (operationShape of operationShapeNames) {
// traverse the tree and store visited shapes
visitRelatedShapeNames(operationShape, shapeMap);
}
// iterate over the shapeMap and remove any shape that wasn't visited
var shapeNames = Object.keys(shapeMap);
for (var name of shapeNames) {
if (!shapeMap[name].visited) {
delete shapeMap[name];
}
}
};
module.exports = {
pruneShapes: pruneShapes
};

View File

@@ -0,0 +1,54 @@
/**
* Removes operations from the model if they require event streams.
* Specifically looks at input and output shapes.
* @param {Object} model - JSON parsed API model (*.normal.json)
*/
function removeEventStreamOperations(model) {
var modifiedModel = false;
// loop over all operations
var operations = model.operations;
var operationNames = Object.keys(operations);
for (var i = 0; i < operationNames.length; i++) {
var operationName = operationNames[i];
var operation = operations[operationName];
// check input and output shapes
var inputShapeName = operation.input && operation.input.shape;
var outputShapeName = operation.output && operation.output.shape;
var requiresEventStream = false;
if (inputShapeName && hasEventStream(model.shapes[inputShapeName], model)) {
requiresEventStream = true;
}
if (requiresEventStream) {
modifiedModel = true;
// remove the operation from the model
console.log('Removing ' + operationName + ' because it depends on event streams on input.');
delete model.operations[operationName];
}
}
return modifiedModel;
}
function hasEventStream(shape, model) {
if (shape.eventstream) {
return true;
} else {
// check each member shape
var memberNames = Object.keys(shape.members);
for (var i = 0; i < memberNames.length; i++) {
var member = shape.members[memberNames[i]];
if (member.eventstream) {
return true;
}
var memberShape = model.shapes[member.shape];
if (memberShape.eventstream) {
return true;
}
}
}
}
module.exports = {
removeEventStreamOperations: removeEventStreamOperations
};

View File

@@ -0,0 +1,85 @@
const { exec, spawn } = require('child_process');
/**
* wrap child_process.exec with retry. Will throw immediately when return
* code not 0; Used for trivial commands since error detail won't be printed.
* @param {string} command
* @param {structure} execOptoins
* @param {number} retryCount
*/
async function executeCommand(command, execOptoins = {}, retryCount = 1) {
try {
const execCommand = command.join(' ');
const { stderr, stdout } = await execute(execCommand, execOptoins);
if (stderr) process.stderr.write(stderr.toString());
if (stdout) process.stderr.write(stdout.toString());
} catch (error) {
if (retryCount > 0) {
await executeCommand(command, execOptoins, --retryCount);
} else {
throw error;
}
}
}
function execute(command, options) {
return new Promise((resolve, reject) => {
exec(command, options, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({
stdout: stdout,
stderr: stderr
});
}
});
})
}
/**
* wrap child_process.spawn with retry
* @param {string} command
* @param {structure} execOptions
* @param {number} retryCount
*/
async function executeLongProcessCommand(command, execOptions = {}, retryCount = 1) {
try {
const firstCommand = command[0];
const options = command.slice(1);
await promisifiedSpawn(firstCommand, options, execOptions);
} catch (error) {
if (retryCount > 0) {
await executeLongProcessCommand(command, execOptions, --retryCount);
} else {
throw error;
}
}
}
function promisifiedSpawn(command, options, execOptions) {
return new Promise((resolve, reject) => {
const subProcess = spawn(command, options, execOptions);
subProcess.stdout.on('data', (data) => {
process.stdout.write(data.toString());
});
subProcess.stderr.on('data', (data) => {
process.stderr.write(data.toString());
});
subProcess.on('error', (err) => {
console.error('spawn error: ', err);
});
subProcess.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(`"${command} ${options.join(' ')}" exited with code: ${code}`);
}
});
});
}
module.exports = {
execute: executeCommand,
executeLongProcess: executeLongProcessCommand,
}

View File

@@ -0,0 +1,160 @@
/* A couple of utility methods */
function each(obj, iter) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) iter(key, obj[key]);
}
}
function nextString(str) {
return 'S' + (parseInt(str.substr(1), 36) + 1).toString(36);
}
/* End utility methods */
function Translator(api, options) {
var origLength = JSON.stringify(api, null, 2).length;
var debugInfo = {flattened: {}, pruned: {}};
var shapeName = 'S0';
var shapeNameMap = {};
var visitedShapes = {};
function logResults() {
console.log('** Generated', api.metadata.endpointPrefix + '-' +
api.metadata.apiVersion +'.min.json' +
(process.env.DEBUG ? ':' : ''));
if (process.env.DEBUG) {
var pruned = Object.keys(debugInfo.pruned);
var flattened = Object.keys(debugInfo.flattened);
var newLength = JSON.stringify(api, null, 2).length;
console.log('- Pruned Shapes:', pruned.length);
console.log('- Flattened Shapes:', flattened.length);
console.log('- Remaining Shapes:', Object.keys(api.shapes).length);
console.log('- Original Size:', origLength / 1024.0, 'kb');
console.log('- Minified Size:', newLength / 1024.0, 'kb');
console.log('- Size Saving:', (origLength - newLength) / 1024.0, 'kb');
console.log('');
}
}
function deleteTraits(obj) {
if (!options.documentation) {
delete obj.documentation;
delete obj.documentationUrl;
delete obj.errors;
delete obj.min;
delete obj.max;
delete obj.pattern;
delete obj['enum'];
delete obj.box;
}
}
function trackShapeDeclaration(ref) {
if (ref.shape && !shapeNameMap[ref.shape]) {
// found a shape declaration we have not yet visited.
// assign a new generated name in the shapeNameMap & visit it
var oldShapeName = ref.shape;
ref.shape = shapeName = nextString(shapeName);
visitedShapes[shapeName] = api.shapes[oldShapeName];
shapeNameMap[oldShapeName] = {name: shapeName, refs: [ref]};
traverseShapeRef(api.shapes[oldShapeName]);
} else if (ref.shape && shapeNameMap[ref.shape]) {
// we visited this shape before. keep track of this ref and rename
// the referenced declaration to the generated name
var map = shapeNameMap[ref.shape];
map.refs.push(ref);
ref.shape = map.name;
}
}
function pruneShapes() {
// prune shapes visited only once or only have type specifiers
each(shapeNameMap, function(name, map) {
if (Object.keys(visitedShapes[map.name]).join() === 'type' &&
['structure','map','list'].indexOf(visitedShapes[map.name].type) < 0) {
// flatten out the shape (only a scalar type property is on the shape)
for (var i = 0; i < map.refs.length; i++) {
var ref = map.refs[i];
debugInfo.flattened[name] = true;
delete ref.shape;
ref.type = visitedShapes[map.name].type;
// string type is default, don't need to specify this
if (ref.type === 'string') delete ref.type;
}
// we flattened all refs, we can prune the shape too
delete visitedShapes[map.name];
debugInfo.pruned[name] = true;
} else if (map.refs.length === 1) { // only visited once
// merge shape data onto ref
var shape = visitedShapes[map.name];
for (var i = 0; i < map.refs.length; i++) {
delete map.refs[i].shape;
for (var prop in shape) {
if (shape.hasOwnProperty(prop)) {
//Translator prefers timestampFormat trait in members rather than in shape
if (map.refs[i].hasOwnProperty(prop) && ['timestampFormat'].indexOf(prop) >= 0) {
continue;
}
map.refs[i][prop] = shape[prop];
}
}
}
// delete the visited shape
delete visitedShapes[map.name];
debugInfo.pruned[name] = true;
}
});
}
function traverseShapeRef(ref) {
if (!ref) return;
deleteTraits(ref);
traverseShapeRef(ref.key); // for maps
traverseShapeRef(ref.value); // for maps
traverseShapeRef(ref.member); // for lists
// for structures
each(ref.members || {}, function(key, value) { traverseShapeRef(value); });
// resolve shape declarations
trackShapeDeclaration(ref);
}
function traverseOperation(op) {
deleteTraits(op);
delete op.name;
if (op.http) {
if (op.http.method === 'POST') delete op.http.method;
if (op.http.requestUri === '/') delete op.http.requestUri;
if (Object.keys(op.http).length === 0) delete op.http;
}
traverseShapeRef(op.input);
traverseShapeRef(op.output);
}
function traverseApi() {
deleteTraits(api);
each(api.operations, function(name, op) { traverseOperation(op); });
api.shapes = visitedShapes;
}
traverseApi();
pruneShapes();
logResults();
return api;
}
module.exports = Translator;

View File

@@ -0,0 +1,75 @@
{
"cloudfront": [
{
"path": "lib/cloudfront/signer",
"imports": [
{
"name": "Signer",
"alias": "signer"
}
]
}
],
"dynamodb": [
{
"path": "lib/dynamodb/document_client",
"imports": [
{
"name": "DocumentClient",
"alias": "document_client"
}
]
},
{
"path": "lib/dynamodb/converter",
"imports": [
{
"name": "Converter",
"alias": "converter"
}
]
}
],
"polly": [
{
"path": "lib/polly/presigner",
"imports": [
{
"name": "Presigner",
"alias": "presigner"
}
]
}
],
"rds": [
{
"path": "lib/rds/signer",
"imports": [
{
"name": "Signer",
"alias": "signer"
}
]
}
],
"s3": [
{
"path": "lib/s3/managed_upload",
"imports": [
{
"name": "ManagedUpload",
"alias": "managed_upload"
}
]
},
{
"path": "lib/s3/presigned_post",
"imports": [
{
"name": "PresignedPost",
"alias": "presigned_post"
}
]
}
]
}

View File

@@ -0,0 +1,686 @@
var fs = require('fs');
var path = require('path');
var pruneShapes = require('./prune-shapes').pruneShapes;
var CUSTOM_CONFIG_ENUMS = {
DUALSTACK: {
FILE_NAME: 'config_use_dualstack',
INTERFACE: 'UseDualstackConfigOptions'
}
};
function TSGenerator(options) {
this._sdkRootDir = options.SdkRootDirectory || process.cwd();
this._apiRootDir = path.join(this._sdkRootDir, 'apis');
this._metadataPath = path.join(this._apiRootDir, 'metadata.json');
this._clientsDir = path.join(this._sdkRootDir, 'clients');
// Lazy loading values on usage to avoid side-effects in constructor
this.metadata = null;
this.typings = {};
this.streamTypes = {};
}
/**
* Loads the AWS SDK metadata.json file.
*/
TSGenerator.prototype.loadMetadata = function loadMetadata() {
var metadataFile = fs.readFileSync(this._metadataPath);
this.metadata = JSON.parse(metadataFile);
return this.metadata;
};
/**
* Modifies metadata to include api model filenames.
*/
TSGenerator.prototype.fillApiModelFileNames = function fillApiModelFileNames() {
var modelPaths = fs.readdirSync(this._apiRootDir);
if (!this.metadata) {
this.loadMetadata();
}
var metadata = this.metadata;
// sort paths so latest versions appear first
modelPaths = modelPaths.sort(function sort(a, b) {
if (a < b) {
return 1;
} else if (a > b) {
return -1;
} else {
return 0;
}
});
// Only get latest version of models
var foundModels = Object.create(null);
modelPaths.forEach(function(modelFileName) {
var match = modelFileName.match(/^(.+)(-[\d]{4}-[\d]{2}-[\d]{2})\.normal\.json$/i);
if (match) {
var model = match[1];
// add version
var version = match[2].substring(1);
if (!foundModels[model]) {
foundModels[model] = {
latestFileName: modelFileName,
versions: [version]
};
} else {
foundModels[model].versions.push(version);
}
}
});
// now update the metadata
var keys = Object.keys(metadata);
keys.forEach(function(key) {
var modelName = metadata[key].prefix || key;
var modelInfo = foundModels[modelName];
metadata[key].api_path = modelInfo.latestFileName;
// find waiters file
var baseName = modelInfo.latestFileName.split('.')[0];
if (modelPaths.indexOf(baseName + '.waiters2.json') >= 0) {
metadata[key].waiters_path = baseName + '.waiters2.json';
}
// add versions
if (!metadata[key].versions) {
metadata[key].versions = [];
}
metadata[key].versions = [].concat(metadata[key].versions, modelInfo.versions);
});
};
TSGenerator.prototype.updateDynamoDBDocumentClient = function updateDynamoDBDocumentClient() {
// read in document client customization
var docClientCustomCode = fs.readFileSync(path.join(this._sdkRootDir, 'lib', 'dynamodb', 'document_client.d.ts')).toString();
var lines = docClientCustomCode.split('\n');
var namespaceIndexStart = -1;
var namespaceIndexEnd = -1;
for (var i = 0, iLen = lines.length; i < iLen; i++) {
var line = lines[i];
// find exported namespace
if (line.indexOf('//<!--auto-generated start-->') >= 0) {
namespaceIndexStart = i;
}
if (line.indexOf('//<!--auto-generated end-->') >= 0) {
namespaceIndexEnd = i;
break;
}
}
if (namespaceIndexStart >= 0 && namespaceIndexEnd >= 0) {
// insert doc client interfaces
lines.splice(namespaceIndexStart + 1, (namespaceIndexEnd - namespaceIndexStart - 1), this.generateDocumentClientInterfaces(1));
var code = lines.join('\n');
this.writeTypingsFile('document_client', path.join(this._sdkRootDir, 'lib', 'dynamodb'), code);
}
};
/**
* Generates the file containing DocumentClient interfaces.
*/
TSGenerator.prototype.generateDocumentClientInterfaces = function generateDocumentClientInterfaces(tabCount) {
tabCount = tabCount || 0;
var self = this;
// get the dynamodb model
var dynamodbModel = this.loadServiceApi('dynamodb');
var code = '';
// stub Blob interface
code += this.tabs(tabCount) + 'interface Blob {}\n';
// generate shapes
var modelShapes = dynamodbModel.shapes;
// iterate over each shape
var shapeKeys = Object.keys(modelShapes);
shapeKeys.forEach(function (shapeKey) {
var modelShape = modelShapes[shapeKey];
// ignore exceptions
if (modelShape.exception) {
return;
}
// overwrite AttributeValue
if (shapeKey === 'AttributeValue') {
code += self.generateDocString('A JavaScript object or native type.', tabCount);
code += self.tabs(tabCount) + 'export type ' + shapeKey + ' = any;\n';
return;
}
code += self.generateTypingsFromShape(dynamodbModel, shapeKey, modelShape, tabCount, []);
});
return code;
};
/**
* Returns a service model based on the serviceIdentifier.
*/
TSGenerator.prototype.loadServiceApi = function loadServiceApi(serviceIdentifier) {
// first, find the correct identifier
var metadata = this.metadata;
var serviceFilePath = path.join(this._apiRootDir, metadata[serviceIdentifier].api_path);
var serviceModelFile = fs.readFileSync(serviceFilePath);
var serviceModel = JSON.parse(serviceModelFile);
// load waiters file if it exists
var waiterFilePath;
if (metadata[serviceIdentifier].waiters_path) {
waiterFilePath = path.join(this._apiRootDir, metadata[serviceIdentifier].waiters_path);
var waiterModelFile = fs.readFileSync(waiterFilePath);
var waiterModel = JSON.parse(waiterModelFile);
serviceModel.waiters = waiterModel.waiters;
}
return serviceModel;
};
/**
* Determines if a member is required by checking for it in a list.
*/
TSGenerator.prototype.checkRequired = function checkRequired(list, member) {
if (list.indexOf(member) >= 0) {
return true;
}
return false;
};
/**
* Generates whitespace based on the count.
*/
TSGenerator.prototype.tabs = function tabs(count) {
var code = '';
for (var i = 0; i < count; i++) {
code += ' ';
}
return code;
};
/**
* Transforms documentation string to a more readable format.
*/
TSGenerator.prototype.transformDocumentation = function transformDocumentation(documentation) {
if (!documentation) {
return '';
}
documentation = documentation.replace(/<(?:.|\n)*?>/gm, '');
documentation = documentation.replace(/\*\//g, '*');
return documentation;
};
/**
* Returns a doc string based on the supplied documentation.
* Also tabs the doc string if a count is provided.
*/
TSGenerator.prototype.generateDocString = function generateDocString(documentation, tabCount) {
tabCount = tabCount || 0;
var code = '';
code += this.tabs(tabCount) + '/**\n';
code += this.tabs(tabCount) + ' * ' + this.transformDocumentation(documentation) + '\n';
code += this.tabs(tabCount) + ' */\n';
return code;
};
/**
* Returns an array of custom configuration options based on a service identiffier.
* Custom configuration options are determined by checking the metadata.json file.
*/
TSGenerator.prototype.generateCustomConfigFromMetadata = function generateCustomConfigFromMetadata(serviceIdentifier) {
// some services have additional configuration options that are defined in the metadata.json file
// i.e. dualstackAvailable = useDualstack
// create reference to custom options
var customConfigurations = [];
var serviceMetadata = this.metadata[serviceIdentifier];
// loop through metadata members
for (var memberName in serviceMetadata) {
if (!serviceMetadata.hasOwnProperty(memberName)) {
continue;
}
// check configs
switch (memberName) {
case 'dualstackAvailable':
customConfigurations.push(CUSTOM_CONFIG_ENUMS.DUALSTACK);
break;
}
}
return customConfigurations;
};
TSGenerator.prototype.generateSafeShapeName = function generateSafeShapeName(name, blacklist) {
blacklist = blacklist || [];
if (blacklist.indexOf(name) >= 0) {
return '_' + name;
}
return name;
};
TSGenerator.prototype.extractTypesDependOnStream = function extractTypesDependOnStream(shapeKey, modelShape) {
var streamTypeList = [];
if (typeof modelShape !== "object" || Object.keys(modelShape).length === 0) {
return [];
}
if (modelShape.streaming) {
streamTypeList.push(shapeKey);
return streamTypeList;
}
for (var subModelKey in modelShape) {
var subModel = modelShape[subModelKey];
var subStreamTypeList = this.extractTypesDependOnStream(subModelKey, subModel);
if (Object.keys(subStreamTypeList).length !== 0) {
for (var streamType of subStreamTypeList) {
streamTypeList.push(streamType);
}
}
}
return streamTypeList;
}
TSGenerator.prototype.addReadableType = function addReadableType(shapeKey) {
var code = '';
if (this.streamTypes[shapeKey]) {
code += '|Readable';
} else if (shapeKey[0] === '_' && this.streamTypes[shapeKey.slice(1)]) {
code += '|Readable';
}
return code;
}
/**
* Generates a type or interface based on the shape.
*/
TSGenerator.prototype.generateTypingsFromShape = function generateTypingsFromShape(model, shapeKey, shape, tabCount, customClassNames) {
// some shapes shouldn't be generated if they are javascript primitives
var jsPrimitives = ['string', 'boolean', 'number'];
if (jsPrimitives.indexOf(shapeKey) >= 0) {
return '';
}
if (['Date', 'Blob'].indexOf(shapeKey) >= 0) {
shapeKey = '_' + shapeKey;
}
// In at least one (cloudfront.Signer) case, a class on a service namespace clashes with a shape
shapeKey = this.generateSafeShapeName(shapeKey, customClassNames);
var self = this;
var code = '';
tabCount = tabCount || 0;
var tabs = this.tabs;
var type = shape.type;
if (shape.eventstream) {
// eventstreams MUST be structures
var members = Object.keys(shape.members);
var events = members.map(function(member) {
// each member is an individual event type, so each must be optional
return member + '?:' + shape.members[member].shape;
});
return code += tabs(tabCount) + 'export type ' + shapeKey + ' = EventStream<{' + events.join(',') + '}>;\n';
}
if (type === 'structure') {
if (shape.isDocument) {
return code += tabs(tabCount) + 'export type ' + shapeKey + ' = DocumentType;\n'
}
code += tabs(tabCount) + 'export interface ' + shapeKey + ' {\n';
var members = shape.members;
// cycle through members
var memberKeys = Object.keys(members);
memberKeys.forEach(function(memberKey) {
// docs
var member = members[memberKey];
if (member.documentation) {
code += self.generateDocString(member.documentation, tabCount + 1);
}
var required = self.checkRequired(shape.required || [], memberKey) ? '' : '?';
var memberType = member.shape;
if (member.eventpayload) {
// eventpayloads are always either structures, or buffers
if (['blob', 'binary'].indexOf(model.shapes[memberType].type) >= 0) {
memberType = 'Buffer';
}
}
memberType = self.generateSafeShapeName(memberType, [].concat(customClassNames, ['Date', 'Blob']));
code += tabs(tabCount + 1) + memberKey + required + ': ' + memberType + ';\n';
});
code += tabs(tabCount) + '}\n';
} else if (type === 'list') {
code += tabs(tabCount) + 'export type ' + shapeKey + ' = ' + this.generateSafeShapeName(shape.member.shape, customClassNames) + '[];\n';
} else if (type === 'map') {
code += tabs(tabCount) + 'export type ' + shapeKey + ' = {[key: string]: ' + this.generateSafeShapeName(shape.value.shape, customClassNames) + '};\n';
} else if (type === 'string' || type === 'character') {
var stringType = 'string';
if (Array.isArray(shape.enum)) {
stringType = shape.enum.map(function(s) {
return '"' + s + '"';
}).join('|') + '|' + stringType;
}
code += tabs(tabCount) + 'export type ' + shapeKey + ' = ' + stringType + ';\n';
} else if (['double', 'long', 'short', 'biginteger', 'bigdecimal', 'integer', 'float'].indexOf(type) >= 0) {
code += tabs(tabCount) + 'export type ' + shapeKey + ' = number;\n';
} else if (type === 'timestamp') {
code += tabs(tabCount) + 'export type ' + shapeKey + ' = Date;\n';
} else if (type === 'boolean') {
code += tabs(tabCount) + 'export type ' + shapeKey + ' = boolean;\n';
} else if (type === 'blob' || type === 'binary') {
code += tabs(tabCount) + 'export type ' + shapeKey + ' = Buffer|Uint8Array|Blob|string'
+ self.addReadableType(shapeKey)
+';\n';
}
return code;
};
/**
* Generates a class method type for an operation.
*/
TSGenerator.prototype.generateTypingsFromOperations = function generateTypingsFromOperations(className, operation, operationName, tabCount) {
var code = '';
tabCount = tabCount || 0;
var tabs = this.tabs;
var input = operation.input;
var output = operation.output;
operationName = operationName.charAt(0).toLowerCase() + operationName.substring(1);
var inputShape = input ? className + '.Types.' + input.shape : '{}';
var outputShape = output ? className + '.Types.' + output.shape : '{}';
if (input) {
code += this.generateDocString(operation.documentation, tabCount);
code += tabs(tabCount) + operationName + '(params: ' + inputShape + ', callback?: (err: AWSError, data: ' + outputShape + ') => void): Request<' + outputShape + ', AWSError>;\n';
}
code += this.generateDocString(operation.documentation, tabCount);
code += tabs(tabCount) + operationName + '(callback?: (err: AWSError, data: ' + outputShape + ') => void): Request<' + outputShape + ', AWSError>;\n';
return code;
};
TSGenerator.prototype.generateConfigurationServicePlaceholders = function generateConfigurationServicePlaceholders() {
/**
* Should create a config service placeholder
*/
var self = this;
var metadata = this.metadata;
// Iterate over every service
var serviceIdentifiers = Object.keys(metadata);
var code = '';
var configCode = '';
var versionsCode = '';
code += 'import * as AWS from \'../clients/all\';\n';
configCode += 'export abstract class ConfigurationServicePlaceholders {\n';
versionsCode += 'export interface ConfigurationServiceApiVersions {\n';
serviceIdentifiers.forEach(function(serviceIdentifier) {
var className = self.metadata[serviceIdentifier].name;
configCode += self.tabs(1) + serviceIdentifier + '?: AWS.' + className + '.Types.ClientConfiguration;\n';
versionsCode += self.tabs(1) + serviceIdentifier + '?: AWS.' + className + '.Types.apiVersion;\n';
});
configCode += '}\n';
versionsCode += '}\n';
code += configCode + versionsCode;
this.writeTypingsFile('config_service_placeholders', path.join(this._sdkRootDir, 'lib'), code);
};
TSGenerator.prototype.getServiceApiVersions = function generateServiceApiVersions(serviceIdentifier) {
var metadata = this.metadata;
var versions = metadata[serviceIdentifier].versions || [];
// transform results (to get rid of '*' and sort
versions = versions.map(function(version) {
return version.replace('*', '');
}).sort();
return versions;
};
/**
* Generates class method types for a waiter.
*/
TSGenerator.prototype.generateTypingsFromWaiters = function generateTypingsFromWaiters(className, waiterState, waiter, underlyingOperation, tabCount) {
var code = '';
tabCount = tabCount || 0;
var operationName = waiter.operation.charAt(0).toLowerCase() + waiter.operation.substring(1);
waiterState = waiterState.charAt(0).toLowerCase() + waiterState.substring(1);
var docString = 'Waits for the ' + waiterState + ' state by periodically calling the underlying ' + className + '.' + operationName + 'operation every ' + waiter.delay + ' seconds (at most ' + waiter.maxAttempts + ' times).';
if (waiter.description) {
docString += ' ' + waiter.description;
}
// get input and output
var inputShape = '{}';
var outputShape = '{}';
if (underlyingOperation.input) {
inputShape = className + '.Types.' + underlyingOperation.input.shape;
}
if (underlyingOperation.output) {
outputShape = className + '.Types.' + underlyingOperation.output.shape;
}
code += this.generateDocString(docString, tabCount);
code += this.tabs(tabCount) + 'waitFor(state: "' + waiterState + '", params: ' + inputShape + ' & {$waiter?: WaiterConfiguration}, callback?: (err: AWSError, data: ' + outputShape + ') => void): Request<' + outputShape + ', AWSError>;\n';
code += this.generateDocString(docString, tabCount);
code += this.tabs(tabCount) + 'waitFor(state: "' + waiterState + '", callback?: (err: AWSError, data: ' + outputShape + ') => void): Request<' + outputShape + ', AWSError>;\n';
return code;
};
/**
* Returns whether a service has customizations to include.
*/
TSGenerator.prototype.includeCustomService = function includeCustomService(serviceIdentifier) {
// check services directory
var servicesDir = path.join(this._sdkRootDir, 'lib', 'services');
var fileNames = fs.readdirSync(servicesDir);
fileNames = fileNames.filter(function(fileName) {
return fileName === serviceIdentifier + '.d.ts';
});
return !!fileNames.length;
};
/**
* Generates typings for classes that live on a service client namespace.
*/
TSGenerator.prototype.generateCustomNamespaceTypes = function generateCustomNamespaceTypes(serviceIdentifier, className) {
var self = this;
var tsCustomizationsJson = require('./ts-customizations');
var customClasses = [];
var code = '';
var serviceInfo = tsCustomizationsJson[serviceIdentifier] || null;
// exit early if no customizations found
if (!serviceInfo) {
return null;
}
code += 'declare namespace ' + className + ' {\n';
//generate import code
var importCode = '';
serviceInfo.forEach(function(data) {
var aliases = [];
var imports = data.imports || [];
imports.forEach(function(pair) {
aliases.push(pair.name + ' as ' + pair.alias);
code += self.tabs(1) + 'export import ' + pair.name + ' = ' + pair.alias + ';\n';
customClasses.push(pair.name);
});
if (aliases.length) {
importCode += 'import {' + aliases.join(', ') + '} from \'../' + data.path + '\';\n';
}
});
code += '}\n';
return {
importCode: importCode,
namespaceCode: code,
customClassNames: customClasses
};
};
TSGenerator.prototype.containsEventStreams = function containsEventStreams(model) {
var shapeNames = Object.keys(model.shapes);
for (var name of shapeNames) {
if (model.shapes[name].eventstream) {
return true;
}
}
return false;
};
TSGenerator.prototype.containsDocumentType = function containsDocumentType(model) {
var shapeNames = Object.keys(model.shapes);
for (var name of shapeNames) {
if (model.shapes[name].isDocument) {
return true;
}
}
return false;
};
/**
* Generates the typings for a service based on the serviceIdentifier.
*/
TSGenerator.prototype.processServiceModel = function processServiceModel(serviceIdentifier) {
var model = this.loadServiceApi(serviceIdentifier);
pruneShapes(model);
var self = this;
var code = '';
var className = this.metadata[serviceIdentifier].name;
var customNamespaces = this.generateCustomNamespaceTypes(serviceIdentifier, className);
var customClassNames = customNamespaces ? customNamespaces.customClassNames : [];
var waiters = model.waiters || Object.create(null);
var waiterKeys = Object.keys(waiters);
// generate imports
code += 'import {Request} from \'../lib/request\';\n';
code += 'import {Response} from \'../lib/response\';\n';
code += 'import {AWSError} from \'../lib/error\';\n';
var hasCustomizations = this.includeCustomService(serviceIdentifier);
var parentClass = hasCustomizations ? className + 'Customizations' : 'Service';
if (hasCustomizations) {
code += 'import {' + parentClass + '} from \'../lib/services/' + serviceIdentifier + '\';\n';
} else {
code += 'import {' + parentClass + '} from \'../lib/service\';\n';
}
if (waiterKeys.length) {
code += 'import {WaiterConfiguration} from \'../lib/service\';\n';
}
code += 'import {ServiceConfigurationOptions} from \'../lib/service\';\n';
// get any custom config options
var customConfig = this.generateCustomConfigFromMetadata(serviceIdentifier);
var hasCustomConfig = !!customConfig.length;
var customConfigTypes = ['ServiceConfigurationOptions'];
code += 'import {ConfigBase as Config} from \'../lib/config-base\';\n';
if (hasCustomConfig) {
// generate import statements and custom config type
customConfig.forEach(function(config) {
code += 'import {' + config.INTERFACE + '} from \'../lib/' + config.FILE_NAME + '\';\n';
customConfigTypes.push(config.INTERFACE);
});
}
if (this.containsEventStreams(model)) {
code += 'import {EventStream} from \'../lib/event-stream/event-stream\';\n';
}
if (this.containsDocumentType(model)) {
code += 'import {DocumentType} from \'../lib/model\';\n';
}
// import custom namespaces
if (customNamespaces) {
code += customNamespaces.importCode;
}
code += 'interface Blob {}\n';
// generate methods
var modelOperations = model.operations;
var operationKeys = Object.keys(modelOperations);
code += 'declare class ' + className + ' extends ' + parentClass + ' {\n';
// create constructor
code += this.generateDocString('Constructs a service object. This object has one method for each API operation.', 1);
code += this.tabs(1) + 'constructor(options?: ' + className + '.Types.ClientConfiguration' + ')\n';
code += this.tabs(1) + 'config: Config & ' + className + '.Types.ClientConfiguration' + ';\n';
operationKeys.forEach(function (operationKey) {
code += self.generateTypingsFromOperations(className, modelOperations[operationKey], operationKey, 1);
});
// generate waitFor methods
waiterKeys.forEach(function (waitersKey) {
var waiter = waiters[waitersKey];
var operation = modelOperations[waiter.operation];
code += self.generateTypingsFromWaiters(className, waitersKey, waiter, operation, 1);
});
code += '}\n';
// check for static classes on namespace
if (customNamespaces) {
code += customNamespaces.namespaceCode;
}
// shapes should map to interfaces
var modelShapes = model.shapes;
// iterate over each shape
var shapeKeys = Object.keys(modelShapes);
code += 'declare namespace ' + className + ' {\n';
// preprocess shapes to fetch out needed dependency. e.g. "streaming": true
shapeKeys.forEach(function (shapeKey) {
var modelShape = modelShapes[shapeKey];
var streamTypeList = self.extractTypesDependOnStream(shapeKey, modelShape);
for (var streamType of streamTypeList) {
self.streamTypes[streamType] = true;
}
});
shapeKeys.forEach(function (shapeKey) {
var modelShape = modelShapes[shapeKey];
code += self.generateTypingsFromShape(model, shapeKey, modelShape, 1, customClassNames);
});
//add extra dependencies like 'streaming'
if (Object.keys(self.streamTypes).length !== 0) {
var insertPos = code.indexOf('interface Blob {}');
code = code.slice(0, insertPos) + 'import {Readable} from \'stream\';\n' + code.slice(insertPos);
}
this.streamTypes = {};
code += this.generateDocString('A string in YYYY-MM-DD format that represents the latest possible API version that can be used in this service. Specify \'latest\' to use the latest possible version.', 1);
code += this.tabs(1) + 'export type apiVersion = "' + this.getServiceApiVersions(serviceIdentifier).join('"|"') + '"|"latest"|string;\n';
code += this.tabs(1) + 'export interface ClientApiVersions {\n';
code += this.generateDocString('A string in YYYY-MM-DD format that represents the latest possible API version that can be used in this service. Specify \'latest\' to use the latest possible version.', 2);
code += this.tabs(2) + 'apiVersion?: apiVersion;\n';
code += this.tabs(1) + '}\n';
code += this.tabs(1) + 'export type ClientConfiguration = ' + customConfigTypes.join(' & ') + ' & ClientApiVersions;\n';
// export interfaces under Types namespace for backwards-compatibility
code += this.generateDocString('Contains interfaces for use with the ' + className + ' client.', 1);
code += this.tabs(1) + 'export import Types = ' + className + ';\n';
code += '}\n';
code += 'export = ' + className + ';\n';
return code;
};
/**
* Write Typings file to the specified directory.
*/
TSGenerator.prototype.writeTypingsFile = function writeTypingsFile(name, directory, code) {
fs.writeFileSync(path.join(directory, name + '.d.ts'), code);
};
/**
* Create the typescript definition files for every service.
*/
TSGenerator.prototype.generateAllClientTypings = function generateAllClientTypings() {
this.fillApiModelFileNames();
var self = this;
var metadata = this.metadata;
// Iterate over every service
var serviceIdentifiers = Object.keys(metadata);
serviceIdentifiers.forEach(function(serviceIdentifier) {
var code = self.processServiceModel(serviceIdentifier);
self.writeTypingsFile(serviceIdentifier, self._clientsDir, code);
});
};
/**
* Create the typescript definition files for the all and browser_default exports.
*/
TSGenerator.prototype.generateGroupedClients = function generateGroupedClients() {
var metadata = this.metadata;
var allCode = '';
var browserCode = '';
// Iterate over every service
var serviceIdentifiers = Object.keys(metadata);
serviceIdentifiers.forEach(function(serviceIdentifier) {
var className = metadata[serviceIdentifier].name;
var code = 'export import ' + className + ' = require(\'./' + serviceIdentifier + '\');\n';
allCode += code;
if (metadata[serviceIdentifier].cors) {
browserCode += code;
}
});
this.writeTypingsFile('all', this._clientsDir, allCode);
this.writeTypingsFile('browser_default', this._clientsDir, browserCode);
};
module.exports = TSGenerator;

View File

@@ -0,0 +1,40 @@
/**
*
* @param {string} startingShape
* @param {{[key: string]: any}} shapeMap
*/
function visitRelatedShapeNames(startingShape, shapeMap) {
var shape = shapeMap[startingShape];
if (shape.visited) {
// exit early if the shape has been visited
return;
}
shape.visited = true;
if (['structure', 'map', 'list'].indexOf(shape.type) < 0) {
// not a complex shape, so it's a terminal shape
return;
}
if (shape.type === 'structure') {
var members = shape.members;
for (var memberName of Object.keys(members)) {
var memberShapeName = members[memberName].shape;
visitRelatedShapeNames(memberShapeName, shapeMap);
}
} else if (shape.type === 'map') {
var keyShape = shape.key.shape;
var valueShape = shape.value.shape;
visitRelatedShapeNames(keyShape, shapeMap);
visitRelatedShapeNames(valueShape, shapeMap);
} else if (shape.type === 'list') {
var memberShape = shape.member.shape;
visitRelatedShapeNames(memberShape, shapeMap);
}
}
module.exports = {
visitRelatedShapeNames: visitRelatedShapeNames
};