Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ jspm_packages
.yarn-integrity

lerna-debug.log
*.js
*.js.map
3 changes: 3 additions & 0 deletions packages/service-model/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js
*.js.map
*.d.ts
212 changes: 212 additions & 0 deletions packages/service-model/__fixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import {ShapeMap} from "../lib/ApiModel/ShapeMap";
import {Shape, Type} from "../lib/ApiModel/Shape";
import {NormalizedModel} from "../lib/TreeModel/types";
import {ServiceMetadata} from "../lib/ApiModel/ServiceMetadata";

export const minimalValidServiceMetadata: ServiceMetadata = {
apiVersion: 'string',
endpointPrefix: 'string',
protocol: 'json',
serviceFullName: 'string',
signatureVersion: 'v4',
uid: 'string',
};

export const minimalShapeMap: ShapeMap & {[key in Type]: Shape} = {
blob: {type: 'blob'},
boolean: {type: 'boolean'},
byte: {type: 'byte'},
character: {type: 'character'},
double: {type: 'double'},
float: {type: 'float'},
integer: {type: 'integer'},
list: {
type: 'list',
member: {shape: 'boolean'},
},
long: {type: 'long'},
map: {
type: 'map',
key: {shape: 'string'},
value: {shape: 'blob'},
},
short: {type: 'short'},
string: {type: 'string'},
structure: {
type: 'structure',
members: {},
},
timestamp: {type: 'timestamp'},
};

export const stringTypes = new Set<Type>([
'character',
'string',
]);

export const numericTypes = new Set<Type>([
'byte',
'double',
'float',
'integer',
'long',
'short',
]);

export const scalarTypes = new Set<Type>([
...numericTypes,
...stringTypes,
'blob',
'boolean',
'timestamp',
]);

export const model: NormalizedModel = {
metadata: {
apiVersion: '2017-04-30',
endpointPrefix: 'endpoint',
protocol: 'rest-json',
serviceFullName: 'AWS Fake Service',
signatureVersion: 'v4',
uid: 'fake-2017-04-30',
},
operations: {
DeleteResource: {
name: 'DeleteResource',
http: {
method: 'DELETE',
requestUri: '/resources/{resourceId}'
},
input: {shape: 'DeleteResourceInput'},
output: {shape: 'DeleteResourceOutput'},
errors: [],
},
GetResource: {
name: 'GetResource',
http: {
method: 'GET',
requestUri: '/resources/{resourceId}'
},
input: {shape: 'GetResourceInput'},
output: {shape: 'GetResourceOutput'},
errors: [
{shape: 'ResourceNotFoundException'},
],
},
PutResource: {
name: 'PutResource',
http: {
method: 'PUT',
requestUri: '/resources/{resourceId}'
},
input: {shape: 'PutResourceInput'},
output: {shape: 'PutResourceOutput'},
errors: [
{shape: 'ValidationException'},
],
},
},
shapes: {
ConsumedCapacity: {
type: 'structure',
members: {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, is it possible to have a structure without any members, aside from an input/output shape?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not explicitly forbidden, but I don't think any services have empty structures. They could be used as sigils (do one thing if an object exists; do another if it is null), but I don't think that's a common pattern.

},
DeleteResourceInput: {
type: 'structure',
required: ['resourceId'],
members: {
resourceId: {shape: 'ResourceId'}
},
topLevel: 'input',
},
DeleteResourceOutput: {
type: 'structure',
required: [],
members: {},
topLevel: 'output',
},
GetResourceInput: {
type: 'structure',
required: ['resourceId'],
members: {
resourceId: {shape: 'ResourceId'}
},
topLevel: 'input',
},
GetResourceOutput: {
type: 'structure',
required: [],
members: {
data: {shape: 'StreamingBlob'},
},
payload: 'data',
topLevel: 'output',
},
NodeId: {type: 'string'},
NodeList: {
type: 'list',
member: {shape: 'NodeId'},
},
PrimaryLocationMap: {
type: 'map',
key: {shape: 'ResourceId'},
value: {shape: 'NodeId'},
},
PutResourceInput: {
type: 'structure',
required: ['resourceId', 'data'],
members: {
resourceId: {shape: 'ResourceId'},
data: {shape: 'StreamingBlob'},
},
topLevel: 'input',
},
PutResourceOutput: {
type: 'structure',
required: [],
members: {
replicatedToNodes: {shape: 'NodeList'},
resourcePrimaries: {shape: 'PrimaryLocationMap'},
consumedCapacity: {shape: 'ConsumedCapacity'},
},
topLevel: 'output',
},
ResourceId: {type: 'string'},
ResourceNotFoundException: {
type: 'structure',
required: [],
members: {},
exception: true,
},
StreamingBlob: {
type: 'blob',
streaming: true,
},
ValidationException: {
type: 'structure',
required: [],
members: {},
exception: true,
},
},
};

// Copies objects and arrays. Only own enumerable properties are preserved.
export function deepCopy<T>(arg: T): T {
if (!arg) {
return arg;
}

if (Array.isArray(arg)) {
return <T><any>arg.map(element => deepCopy(element));
}

if (typeof arg === 'object') {
return <T>Object.keys(arg).reduce((
carry: Partial<T>,
item: keyof T
) => Object.assign(carry, {[item]: deepCopy(arg[item])}), {});
}

return arg;
}
56 changes: 56 additions & 0 deletions packages/service-model/__tests__/ApiModel/ApiModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {ApiModel, isApiModel} from "../../lib/ApiModel";
import {minimalValidServiceMetadata} from "../../__fixtures__";

describe('isApiModel', () => {
const minimalValidApiModel: ApiModel = {
metadata: minimalValidServiceMetadata,
operations: {},
shapes: {},
};

it('should accept a valid ApiModel', () => {
expect(isApiModel(minimalValidApiModel)).toBe(true);
});

it(
'should reject objects where "metadata" is not a valid ServiceMetadata object',
() => {
expect(isApiModel(
Object.assign({}, minimalValidApiModel, {metadata: 'string'})
)).toBe(false);
}
);

it('should reject objects where "operations" is not a valid OperationMap', () => {
expect(isApiModel(
Object.assign({}, minimalValidApiModel, {operations: 'string'})
)).toBe(false);
});

it('should reject objects where "shapes" is not a valid ShapeMap', () => {
expect(isApiModel(
Object.assign({}, minimalValidApiModel, {shapes: 'string'})
)).toBe(false);
});

it('should accept an ApiModel where "documentation" is present and a string', () => {
expect(isApiModel(
Object.assign({}, minimalValidApiModel, {documentation: 'foo'})
)).toBe(true);
});

it(
'should reject objects where a "documentation" property is present and not a string',
() => {
expect(isApiModel(
Object.assign({}, minimalValidApiModel, {documentation: {}})
)).toBe(false);
}
);

it('should reject scalar values', () => {
for (let scalar of [null, void 0, 1, 'string', true]) {
expect(isApiModel(scalar)).toBe(false);
}
});
});
Loading