odata-v4-server
Version:
OData V4 Server
1,101 lines (956 loc) • 34.4 kB
text/typescript
/// <reference types="mocha" />
import { Token } from "odata-v4-parser/lib/lexer";
import { createFilter } from "odata-v4-inmemory";
import { ODataController, ODataServer, ODataProcessor, ODataMethodType, ODataResult, Edm, odata, ODataHttpContext, ODataStream, ODataEntity } from "../lib/index";
import { Product, Category } from "./model/model";
import { ProductPromise, CategoryPromise } from "./model/ModelsForPromise";
import { GeneratorProduct, GeneratorCategory } from "./model/ModelsForGenerator";
import { StreamProduct, StreamCategory } from "./model/ModelsForStream";
import { Readable, PassThrough, Writable } from "stream";
import { ObjectID } from "mongodb";
import { processQueries, doOrderby, doSkip, doTop } from "./utils/queryOptions"
import * as fs from "fs";
import * as path from "path";
import * as streamBuffers from "stream-buffers";
const extend = require("extend");
let categories = require("./model/categories").slice();
let products = require("./model/products").slice();
let categories2 = require("./model/categories").slice();
let products2 = require("./model/products").slice();
const serverCache = [];
if (typeof after == "function"){
after(function(){
serverCache.forEach(server => server.close());
});
}
export class Foobar {
.Key
.Computed
.Int32
id: number
.Int16
a: number
.String
foo: string
.Action
Foo() { }
.Function(Edm.String)
Bar() {
return "foobar";
}
.namespace("Echo")
.Function(Edm.String)
echo( .String message) {
return message;
}
.namespace("Echo")
.Function(Edm.Collection(Edm.String))
echoMany( .String message) {
return [message];
}
}
export class Image {
.Key
.Computed
.Int32
Id: number
.String
Filename: string
.Stream("image/png")
Data: ODataStream
.Stream("image/png")
Data2: ODataStream
}
.MediaEntity("audio/mp3")
export class Music extends PassThrough {
.Key
.Computed
.Int32
Id: number
.String
Artist: string
.String
Title: string
}
let foobarObj = { id: 1, foo: 'bar' };
let foobarObj2 = { id: 2, foo: 'bar', a: 'a' };
.type(Foobar)
export class SyncTestController extends ODataController {
.GET
entitySet(/*@odata.query query:Token, @odata.context context:any, @odata.result result:any, @odata.stream stream:ODataProcessor*/) {
return [{ id: 1, a: 1 }];
}
.GET()
entity( .key() key: number) {
if (key === 1) return ODataResult.Ok(foobarObj);
if (key === 2) return ODataResult.Ok(foobarObj2);
if (key === 999) return ODataResult.Ok({ id: key, foo: "999" });
return ODataResult.Ok({ id: key, foo: "bar" });
}
.method("POST")
insert( .body body: any) {
if (!body.id) body.id = 1;
return body;
}
put( .body body: any) {
body.id = 1;
}
patch( .key key: number, .body delta: any) {
if (key === 2) return Object.assign(foobarObj2, delta);
return Object.assign({
id: key,
foo: "bar",
a: 'a'
}, delta);
}
.PUT('foo')
putProperty( .body body: any, .result _: Foobar) {
foobarObj.foo = body.foo;
}
.PATCH('foo')
patchProperty( .body body: any, .result _: Foobar) {
foobarObj.foo = body.foo;
}
.DELETE('foo')
deleteProperty( .result _: Foobar) {
if (foobarObj.foo) foobarObj.foo = null;
}
.method(ODataMethodType.DELETE)
remove() { }
.Function(Edm.EntityType(Foobar))
getFoo() {
return foobarObj;
}
}
.type(Foobar)
export class GeneratorTestController extends ODataController {
.GET
*entitySet() {
return [{ id: 1, a: 1 }];
}
}
.type(Foobar)
export class AsyncTestController extends ODataController {
.GET
entitySet() {
return new Promise((resolve, reject) => {
try {
setTimeout(() => {
resolve([{ id: 1, a: 1 }]);
});
} catch (err) {
reject(err);
}
});
}
.GET
entity( .key key: number) {
return ODataResult.Ok(new Promise((resolve, reject) => {
try {
setTimeout(() => {
const a: number = 1;
let result = { id: key };
(<any>result).inlinecount = a;
resolve(result);
});
} catch (err) {
reject(err);
}
}));
}
.POST
insert( .body body: any) {
return new Promise((resolve, reject) => {
try {
setTimeout(() => {
body.id = 1;
resolve(body);
});
} catch (err) {
reject(err);
}
});
}
}
.type(Foobar)
export class InlineCountController extends ODataController {
.GET
entitySet() {
let result = [{ id: 1, a: 1 }];
(<any>result).inlinecount = 1;
return result;
}
}
.type(Foobar)
export class BoundOperationController extends ODataController {
.Action
Action() {
return new Promise((resolve) => {
setTimeout(resolve);
});
}
.Function(Edm.String)
Function( .Int16 value: number) {
return `foobar:${value}`;
}
.Function(Edm.String)
FunctionMore( .String message: string, .Int64 value: number) {
return `The number is ${value} and your message was ${message}.`;
}
.GET
entitySet() {
return [{ id: 1, a: 1 }];
}
.GET
entity( .key key: number) {
return { id: key, a: 1 };
}
}
.type(Image)
export class ImagesController extends ODataController {
.GET
entitySet( .query _: Token, .context __: any, .result ___: any, .stream ____: ODataProcessor) {
let image = new Image();
image.Id = 1;
image.Filename = "tmp.png";
return [image];
}
.GET()
entity( .key() key: number) {
let image = new Image();
image.Id = key;
image.Filename = "tmp.png";
return image;
}
.GET("Data")
getData( .key _: number, .context context: ODataHttpContext) {
let globalReadableImgStrBuffer = new streamBuffers.ReadableStreamBuffer();
globalReadableImgStrBuffer.put("tmp.png");
globalReadableImgStrBuffer.stop();
return globalReadableImgStrBuffer;
}
.POST("Data")
postData( .key _: number, .body data: Readable) {
let globalWritableImgStrBuffer = new streamBuffers.WritableStreamBuffer();
return data.pipe(globalWritableImgStrBuffer);
}
.GET("Data2")
getData2( .key _: number, .stream stream: Writable, .context context: ODataHttpContext) {
return new ODataStream(fs.createReadStream(path.join(__dirname, "..", "..", "src", "test", "fixtures", "logo_jaystack.png"))).pipe(context.response);
}
.POST("Data2")
postData2( .key _: number, .body data: Readable) {
return new ODataStream(fs.createWriteStream(path.join(__dirname, "..", "..", "src", "test", "fixtures", "tmp.png"))).write(data);
}
}
let globalWritableMediaStrBuffer = new streamBuffers.WritableStreamBuffer();
let globalReadableMediaStrBuffer = new streamBuffers.ReadableStreamBuffer();
.type(Music)
export class MusicController extends ODataController {
.GET
findAll( .context _: ODataHttpContext) {
let music = new Music();
music.Id = 1;
music.Artist = "Dream Theater";
music.Title = "Six degrees of inner turbulence";
return [music];
}
.GET
findOne( .key() _: number, .context __: ODataHttpContext) {
let music = new Music();
music.Id = 1;
music.Artist = "Dream Theater";
music.Title = "Six degrees of inner turbulence";
return music;
}
.GET.$value
mp3( .key _: number, .context context: ODataHttpContext) {
globalReadableMediaStrBuffer.put(globalWritableMediaStrBuffer.getContents());
return globalReadableMediaStrBuffer.pipe(<Writable>context.response);
}
.POST.$value
post( .key _: number, .body upload: Readable) {
return upload.pipe(globalWritableMediaStrBuffer);
}
}
.type(Product)
export class ProductsController extends ODataController {
.GET
find( .query query: Token) {
const filter = query && query.value && query.value.options && query.value.options.find(t => t.type == "Filter");
if (filter){
return products
.map((product) => Object.assign({}, product, { _id: product._id.toString(), CategoryId: product.CategoryId && product.CategoryId.toString() }))
.filter(createFilter(filter));
}
(<any>products).inlinecount = products.length;
return ODataResult.Ok(
new Promise((resolve, reject) => {
try {
resolve(products);
} catch (error) {
reject(error);
}
})
);
}
.GET
.parameter("key", odata.key)
findOne(key: string): Product {
return products.filter(product => product._id.toString() == key)[0] || null;
}
.GET('Name')
.parameter("key", odata.key)
.parameter("result", odata.result)
getName(key: string, result: Product): string {
return result.Name;
}
.createRef("Category")
.updateRef("Category")
async setCategory( .key key: string, .link('categoryId') link: string): Promise<number> {
return products.filter(product => {
if (product._id.toString() === key) {
product.CategoryId = new ObjectID(link);
return product;
}
return null;
});
}
.deleteRef("Category")
unsetCategoryId( .key key: string, .link link: string): Promise<number> {
return new Promise((resolve, reject) => {
products.filter(product => {
if (product._id.toString() === key) {
product.CategoryId = null;
return product;
}
});
resolve(products);
})
}
}
ProductsController.enableFilter(ProductsController.prototype.find, 'filter');
.type(Category)
export class CategoriesController extends ODataController {
.GET
find( .filter filter: Token): Category[] {
if (filter) return categories.map((category) => Object.assign({}, category, { _id: category._id.toString() })).filter(createFilter(filter));
return categories;
}
.GET
.parameters({
key: odata.key
})
findOne(key: string): Category {
return categories.filter(category => category._id.toString() == key)[0] || null;
}
.POST("Products")
insertProduct( .key key: string, .link link: string, .body body: Product) {
body._id = new ObjectID('578e1a7c12eaebabec4af23c')
return ODataResult.Created(new Promise((resolve, reject) => {
try {
resolve(body);
} catch (err) {
reject(err);
}
}));
}
.GET("Products").$ref
.parameter("key", odata.key)
.parameter("link", odata.link)
findProduct(key: string, link: string): Product {
return products.filter(product => product._id.toString() === link);
}
.POST("Products").$ref
.method("PUT", "Products").$ref
.PATCH("Products").$ref
*setCategory( .key key: string, .link link: string) {
yield products.filter(product => {
if (product._id.toString() === link) {
product.CategoryId = new ObjectID(key);
return product;
}
});
}
.DELETE("Products").$ref
unsetCategory( .key key: string, .link link: string) {
return new Promise(resolve => {
products.filter(product => {
if (product._id.toString() === link) {
product.CategoryId = null;
return product;
}
});
resolve(products);
})
}
}
CategoriesController.enableFilter('find');
export class CategoryStream extends Category{}
.type(CategoryStream)
.EntitySet("Categories")
export class CategoriesStreamingController extends ODataController {
.GET
find( .filter filter: Token, .stream stream: Writable) {
let response = [];
response = categories;
if (filter) response = categories.map((category) => Object.assign({}, category, { _id: category._id.toString() })).filter(createFilter(filter));
response.forEach(c => {
stream.write(c)
});
stream.end();
}
.GET
.parameters({
key: odata.key
})
findOne(key: string) {
return categories.find(category => category._id.toString() == key) || null;
}
.GET("Products")
getProducts( .result result: Category, .stream stream: Writable, .context context: ODataHttpContext) {
const filteredProducts = products.filter(p => p.CategoryId && p.CategoryId.toString() === result._id.toString());
filteredProducts.forEach(p => { stream.write(p) });
stream.end();
}
}
/**
* GENERATOR CONTROLLERS
*/
const toObjectID = _id => _id && !(_id instanceof ObjectID) ? ObjectID.createFromHexString(_id) : _id;
const delay = async function (ms: number): Promise<any> {
return new Promise(resolve => setTimeout(resolve, ms));
};
.Annotate({
term: "UI.DisplayName",
string: "Products2"
})
export class Product2 {
.Key
.Computed
.String
.Convert(toObjectID)
.Annotate({
term: "UI.DisplayName",
string: "Product2 identifier"
}, {
term: "UI.ControlHint",
string: "ReadOnly"
})
_id:ObjectID
.String
.Required
.Convert(toObjectID)
CategoryId:ObjectID
.ForeignKey("CategoryId")
.EntityType(Edm.ForwardRef(() => Category2))
.Partner("Products2")
Category2:Category2
.Boolean
Discontinued:boolean
.String
.Annotate({
term: "UI.DisplayName",
string: "Product2 title"
}, {
term: "UI.ControlHint",
string: "ShortText"
})
Name:string
.String
.Annotate({
term: "UI.DisplayName",
string: "Product2 English name"
}, {
term: "UI.ControlHint",
string: "ShortText"
})
QuantityPerUnit:string
.Decimal
.Annotate({
term: "UI.DisplayName",
string: "Unit price of product2"
}, {
term: "UI.ControlHint",
string: "Decimal"
})
UnitPrice:number
}
.OpenType
.Annotate({
term: "UI.DisplayName",
string: "Categories2"
})
export class Category2 {
.Key
.Computed
.String
.Deserialize(toObjectID)
.Annotate({
term: "UI.DisplayName",
string: "Category2 identifier"
},
{
term: "UI.ControlHint",
string: "ReadOnly"
})
_id:ObjectID
.String
Description:string
.String
.Annotate({
term: "UI.DisplayName",
string: "Category2 name"
},
{
term: "UI.ControlHint",
string: "ShortText"
})
Name:string
.ForeignKey("CategoryId")
.Collection(Edm.EntityType(Product2))
.Partner("Category2")
Products2:Product2[]
.Collection(Edm.String)
.Function
echo(){
return ["echotest"];
}
}
.type(Category2)
.EntitySet("Categories2")
export class CategoriesGeneratorController extends ODataController {
.GET
*find( .filter filter: Token, .stream stream: Writable) {
let response = categories2;
if (filter) response = yield categories2.map((category) => Object.assign({}, category, { _id: category._id.toString() })).filter(createFilter(filter));
stream.write({"@odata.count": response.length});
for (let category of response) {
stream.write(category);
yield delay(1);
};
stream.end();
}
.GET
.parameters({
key: odata.key
})
*findOne(key: string) {
return yield categories2.find(category => category._id.toString() === key) || null;
}
.GET("Products2")
*findProduct( .key key: string, .result result: Category2) {
return yield products2.filter((product) => product.CategoryId && product.CategoryId.toString() === result._id.toString() && product._id.toString() === key.toString());
}
.GET("Products2")
*findProducts( .filter filter: Token, .stream stream: Writable, .result result: Category2) {
let response = products2.map((product) => Object.assign({}, product, { _id: product._id.toString() }));
if (filter) response = response.filter(createFilter(filter));
response = response.filter((product) => product.CategoryId && product.CategoryId.toString() === result._id.toString());
for (let c of response) {
stream.write(c);
yield delay(10);
}
stream.end();
}
}
.type(Product2)
.EntitySet("Products2")
export class ProductsGeneratorController extends ODataController {
.GET
*find( .filter filter: Token, .stream stream: Writable) {
let response = products2;
if (filter) response = yield products2
.map((product) => Object.assign({}, product, { _id: product._id.toString(), CategoryId: product.CategoryId && product.CategoryId.toString() }))
.filter(createFilter(filter));
for (let category of response) {
stream.write(category);
yield delay(1);
};
stream.end();
}
.GET
.parameters({
key: odata.key
})
*findOne(key: string) {
return yield products2.filter(p => p._id.toString() == key)[0] || null;
}
.GET("Category2")
*findCategories( .filter filter: Token, .stream stream: Writable, .result result: any) {
return yield categories2.filter((c) => c && c._id.toString() === result.CategoryId.toString());
}
}
.type(ProductPromise)
export class ProductsPromiseGeneratorController extends ODataController {
.GET
*find( .filter filter: Token) {
if (filter) {
return yield Promise.resolve(products2
.map((product) => Object.assign({}, product, { _id: product._id.toString() }))
.filter(createFilter(filter)));
} else {
(<any>products2).inlinecount = products2.length;
return yield Promise.resolve(products2)
}
}
.GET
.parameters({
key: odata.key
})
*findOne(key: string) {
return yield Promise.resolve(products2.filter(p => p._id.toString() == key)[0] || null);
}
.GET("CategoryPromise")
*findCategories( .filter filter: Token, .result result: ProductPromise) {
return yield Promise.resolve(categories2.filter((c) => c && c._id.toString() === result.CategoryId.toString()));
}
}
.type(CategoryPromise)
export class CategoriesPromiseGeneratorController extends ODataController {
.GET
*find( .filter filter: Token) {
if (filter) {
return yield Promise.resolve(categories2
.map((category) => Object.assign({}, category, { _id: category._id.toString() }))
.filter(createFilter(filter)));
}
return yield Promise.resolve(categories2)
}
.GET
.parameters({
key: odata.key
})
*findOne(key: string) {
return yield Promise.resolve(categories2.find(category => category._id.toString() === key) || null);
}
.GET("ProductPromises")
*findProduct( .key key: string, .result result: CategoryPromise) {
return yield Promise.resolve(products2.filter((product) => product.CategoryId && product.CategoryId.toString() === result._id.toString() && product._id.toString() === key.toString()));
}
.GET("ProductPromises")
*findProducts( .filter filter: Token, .stream stream: Writable, .result result: CategoryPromise) {
return yield Promise.resolve(products2.filter((product) => product.CategoryId && product.CategoryId.toString() === result._id.toString()));
}
}
const getAllProducts = async () => {
return await products2;
}
const getProductsByFilter = async (filter: Token) => {
return await products2
.map((product) => Object.assign({}, product, { _id: product._id.toString(), CategoryId: product.CategoryId && product.CategoryId.toString() }))
.filter(createFilter(filter));
}
const getProductByKey = async (key: string) => {
return await products2.find(p => p._id.toString() == key) || null;
}
const getCategoryOfProduct = async (result: GeneratorProduct) => {
return await categories2.find((c) => c && c._id.toString() === result.CategoryId.toString()) || null;
}
const getCategoryByFilterOfProduct = async (filter: Token, result: GeneratorProduct) => {
return await categories2
.filter(c => c._id.toString() === result.CategoryId.toString())
.map((category) => Object.assign({}, category, { _id: category._id.toString() }))
.filter(createFilter(filter));
}
.type(GeneratorProduct)
export class ProductsAdvancedGeneratorController extends ODataController {
.GET
*find( .filter filter: Token) {
if (filter) return yield getProductsByFilter(filter);
return yield getAllProducts();
}
.GET
.parameters({ key: odata.key })
*findOne(key: string) {
return yield getProductByKey(key);
}
.GET("GeneratorCategory")
*findCategories( .filter filter: Token, .result result: GeneratorProduct) {
if(filter) return yield getCategoryByFilterOfProduct(filter, result);
return yield getCategoryOfProduct(result);
}
}
const getAllCategories = async () => {
return await categories2;
}
const getCategoriesByFilter = async (filter: Token) => {
return await categories2
.map((category) => Object.assign({}, category, { _id: category._id.toString() }))
.filter(createFilter(filter));
}
const getCategoryByKey = async (key: string) => {
return await categories2.find(category => category._id.toString() === key) || null;
}
const getProductOfCategory = async (key: string, result: GeneratorCategory) => {
return await products2.filter((product) => product.CategoryId && product.CategoryId.toString() === result._id.toString() && product._id.toString() === key.toString());
}
const getProductsOfCategory = async (result: GeneratorCategory) => {
return await products2.filter((product) => product.CategoryId && product.CategoryId.toString() === result._id.toString());
}
const getProductsByFilterOfCategory = async (filter: Token, result: GeneratorCategory) => {
return await products2
.filter(p => p.CategoryId.toString() === result._id.toString())
.map((product) => Object.assign({}, product, { _id: product._id.toString(), CategoryId: product.CategoryId && product.CategoryId.toString() }))
.filter(createFilter(filter));
}
.type(GeneratorCategory)
export class CategoriesAdvancedGeneratorController extends ODataController {
.GET
*find(.query query: Token, .filter filter: Token) {
let options = yield processQueries(query);
let response: Category[] = yield getAllCategories()
if (filter) response = yield getCategoriesByFilter(filter);
response = yield doOrderby(response, options);
response = yield doSkip(response, options);
response = yield doTop(response, options);
return response;
}
.GET
.parameters({ key: odata.key })
*findOne(key: string) {
return yield getCategoryByKey(key)
}
.GET("GeneratorProducts")
*filterProducts( .query query: Token, .filter filter: Token, .result result: GeneratorCategory) {
let options = yield processQueries(query);
let response: GeneratorProduct[] = yield getProductsOfCategory(result);
if (filter) response = yield getProductsByFilterOfCategory(filter, result);
response = yield doOrderby(response, options);
response = yield doSkip(response, options);
response = yield doTop(response, options);
return response
}
}
.MediaEntity("image/png")
export class Image2 {
.Key
.Computed
.Int32
Id: number
.String
Filename: string
.Stream("image/png")
Data: ODataStream
.Stream("image/png")
Data2: ODataStream
}
.type(Image2)
export class Images2Controller extends ODataController {
.GET
entitySet( .query _: Token) {
let image2 = new Image2();
image2.Id = 1;
image2.Filename = "tmp.png";
return [image2];
}
.GET()
entity( .key() key: number) {
let image2 = new Image2();
image2.Id = key;
image2.Filename = "tmp.png";
return image2;
}
.GET("Data2")
*getData2( .key _: number, .stream stream: Writable, .context context: ODataHttpContext) {
return yield new ODataStream(fs.createReadStream(path.join(__dirname, "..", "..", "src", "test", "fixtures", "logo_jaystack.png"))).pipe(context.response);
}
.POST("Data2")
*postData2( .key _: number, .body data: Readable) {
return yield new ODataStream(fs.createWriteStream(path.join(__dirname, "..", "..", "src", "test", "fixtures", "tmp.png"))).write(data);
}
}
export class Location {
.String
City: string
.String
Address: string
constructor(city, address) {
this.City = city;
this.Address = address;
}
}
export class User {
.Key
.Int32
Id: number
.ComplexType(Location)
Location: Location
constructor(id, location) {
this.Id = id;
this.Location = location;
}
}
export class UsersController extends ODataController {
.GET
find() {
return [new User(1, new Location("Budapest", "Virág utca"))];
}
.GET
findOne( .key key: number) {
return new User(key, new Location("Budapest", "Virág utca"));
}
.namespace("Session")
.Action
logout() { }
}
export class DefTest extends ODataEntity { }
DefTest.define({
id: [Edm.Int32, Edm.Key, Edm.Computed],
key: Edm.String,
value: Edm.String
});
export class DefTestController extends ODataController {
all() {
return [Object.assign(new DefTest(), {
id: 1,
key: 'testkey',
value: 'testvalue'
})];
}
one(key) {
return Object.assign(new DefTest(), {
id: key,
key: `testkey${key}`,
value: `testvalue${key}`
});
}
}
DefTestController.define(odata.type(DefTest), {
all: odata.GET,
one: [odata.GET, {
key: odata.key
}]
});
export class HeaderTestEntity {
.Int32
.Key
.Required
Id: number
}
.type(HeaderTestEntity)
export class HeaderTestEntityController extends ODataController {
.GET
findAll( .context ctx: ODataHttpContext, .result ___: any, .stream ____: ODataProcessor) {
ctx.response.status(403);
return [];
}
.GET
findOneByKeys( .key key: number, .context ctx: ODataHttpContext) {
ctx.response.sendStatus(500);
return {};
}
}
export class UpsertTestEntity {
.Int32
.Key
.Required
Id: number
.String
name: string
constructor(id?, name?) {
this.Id = id;
this.name = name;
}
}
.type(UpsertTestEntity)
export class UpsertTestEntityController extends ODataController {
.GET
findAll( .context ctx: ODataHttpContext, .result ___: any, .stream ____: ODataProcessor) {
return [new UpsertTestEntity(1, 'upsert')];
}
.GET
findOneByKeys( .id id: number, .context ctx: ODataHttpContext) {
return new UpsertTestEntity(1, 'upsert');
}
put( .body body: any) {
let up = new UpsertTestEntity(1, 'upsert');
if (body.Id && body.Id === 1) {
up.name = body.name;
return null;
}
if (body.Id) {
return new UpsertTestEntity(body.Id, body.name);
}
return new UpsertTestEntity(9999, body.name);
}
}
export class DefTestServer extends ODataServer{}
DefTestServer.define(odata.controller(DefTestController, true));
export class HiddenController extends ODataController { }
.cors
.controller(SyncTestController, "EntitySet")
.controller(GeneratorTestController, "GeneratorEntitySet")
.controller(AsyncTestController, "AsyncEntitySet")
.controller(InlineCountController, "InlineCountEntitySet")
.controller(BoundOperationController, "BoundOperationEntitySet")
.controller(ImagesController, "ImagesControllerEntitySet")
.controller(MusicController, "MusicControllerEntitySet")
.controller(ProductsController, true)
.controller(CategoriesController, true)
.controller(UsersController, true, User)
.controller(HiddenController)
.controller(CategoriesStreamingController, "CategoriesStream")
.controller(CategoriesGeneratorController)
.controller(ProductsGeneratorController)
.controller(ProductsPromiseGeneratorController, "AdvancedProducts")
.controller(CategoriesPromiseGeneratorController, "AdvancedCategories")
.controller(ProductsAdvancedGeneratorController, "GeneratorProducts")
.controller(CategoriesAdvancedGeneratorController, "GeneratorCategories")
.controller(Images2Controller, "Images2ControllerEntitySet")
.controller(HeaderTestEntityController, "HeaderTestEntity")
.controller(UpsertTestEntityController, "UpsertTestEntity")
.container("TestContainer")
export class TestServer extends ODataServer {
.ActionImport
ActionImport() {
return new Promise((resolve) => {
setTimeout(resolve);
});
}
.ActionImport
ActionImportParams( .Int32 value: number) {
if (typeof value != "number") throw new Error("value is not a number!");
}
.FunctionImport(Edm.String)
FunctionImport( .Int64 value: number) {
return `The number is ${value}.`;
}
.FunctionImport(Edm.String)
FunctionImportMore( .String message: string, .Int64 value: number) {
return `The number is ${value} and your message was ${message}.`;
}
.FunctionImport(Edm.String)
SetStatusCode( .context ctx: ODataHttpContext) {
ctx.response.sendStatus(403);
return `The status code is ${ctx.response.statusCode}`;
}
.ActionImport
SetStatusCode2( .context ctx: ODataHttpContext) {
ctx.response.sendStatus(500);
}
}
serverCache.push(TestServer.create(5005));
.namespace("Authentication")
.controller(UsersController, true)
export class AuthenticationServer extends ODataServer {
.namespace("Echo")
.FunctionImport(Edm.String)
echo( .String message: string): string {
return message;
}
}
.cors
.controller(ProductsController, true)
.controller(CategoriesController, false)
export class ProductServer extends ODataServer { }
serverCache.push(ProductServer.create(7001));
.cors
.controller(ProductsController, false)
.controller(CategoriesController, true)
export class CategoryServer extends ODataServer { }
serverCache.push(CategoryServer.create(7002));
export class NoServer extends ODataServer { }
process.on("warning", warning => {
console.log(warning.stack);
});