本文整理汇总了TypeScript中Boom.conflict函数的典型用法代码示例。如果您正苦于以下问题:TypeScript conflict函数的具体用法?TypeScript conflict怎么用?TypeScript conflict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了conflict函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: handler
export default async function handler(request: Request, reply: IReply) {
const requestDoc: AttendeeResource.TopLevelDocument = request.payload
const attendee = new AttendeeModel({
attendeeid: requestDoc.data.id,
})
try {
await attendee.save()
} catch (err) {
if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
reply(Boom.conflict('Attendee already exists'))
return
}
throw err
}
const attendeeResponse: AttendeeResource.TopLevelDocument = {
links: { self: `/attendees/${encodeURIComponent(attendee.attendeeid)}` },
data: {
type: 'attendees',
id: attendee.attendeeid,
},
}
reply(attendeeResponse).code(201)
}
示例2: initPostSpacesApi
export function initPostSpacesApi(server: any, routePreCheckLicenseFn: any) {
server.route({
method: 'POST',
path: '/api/spaces/space',
async handler(request: any, reply: any) {
const { SavedObjectsClient } = server.savedObjects;
const spacesClient: SpacesClient = server.plugins.spaces.spacesClient.getScopedClient(
request
);
const space = request.payload;
try {
return reply(await spacesClient.create(space));
} catch (error) {
if (SavedObjectsClient.errors.isConflictError(error)) {
return reply(Boom.conflict(`A space with the identifier ${space.id} already exists.`));
}
return reply(wrapError(error));
}
},
config: {
validate: {
payload: spaceSchema,
},
pre: [routePreCheckLicenseFn],
},
});
}
示例3: initPostSpacesApi
export function initPostSpacesApi(server: any, routePreCheckLicenseFn: any) {
server.route({
method: 'POST',
path: '/api/spaces/space',
async handler(request: any) {
server.log(['spaces', 'debug'], `Inside POST /api/spaces/space`);
const { SavedObjectsClient } = server.savedObjects;
const spacesClient: SpacesClient = server.plugins.spaces.spacesClient.getScopedClient(
request
);
const space = request.payload;
try {
server.log(['spaces', 'debug'], `Attempting to create space`);
return await spacesClient.create(space);
} catch (error) {
if (SavedObjectsClient.errors.isConflictError(error)) {
return Boom.conflict(`A space with the identifier ${space.id} already exists.`);
}
server.log(['spaces', 'debug'], `Error creating space: ${error}`);
return wrapError(error);
}
},
config: {
validate: {
payload: spaceSchema,
},
pre: [routePreCheckLicenseFn],
},
});
}
示例4: handler
export default async function handler(req: Request, reply: IReply) {
const requestDoc: ChallengeResource.TopLevelDocument = req.payload
const challenge = new ChallengeModel({
challengeid: slugify(requestDoc.data.attributes.name),
name: requestDoc.data.attributes.name,
members: [],
})
try {
await challenge.save()
} catch (err) {
if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
reply(Boom.conflict('Challenge already exists'))
return
}
throw err
}
const challengeResponse: ChallengeResource.TopLevelDocument = {
links: {
self: `/challenges/${encodeURIComponent(challenge.challengeid)}`,
},
data: {
type: 'challenges',
id: challenge.challengeid,
attributes: {
name: challenge.name,
},
},
}
reply(challengeResponse).code(201)
}
示例5: handler
export default async function handler(req: Request, reply: IReply) {
const requestDoc: UserResource.TopLevelDocument = req.payload
const user = new UserModel({
userid: requestDoc.data.id,
name: requestDoc.data.attributes.name,
})
try {
await user.save()
} catch (err) {
if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
reply(Boom.conflict('User already exists'))
return
}
throw err
}
const userResponse: UserResource.TopLevelDocument = {
links: {
self: `/users/${encodeURIComponent(user.userid)}`,
},
data: {
type: 'users',
id: user.userid,
attributes: {
name: user.name,
},
relationships: {
team: {
links: {
self: `/users/${encodeURIComponent(user.userid)}/team`,
},
data: null,
},
},
},
}
const eventBroadcaster: EventBroadcaster = req.server.app.eventBroadcaster
eventBroadcaster.trigger('users_add', {
userid: user.userid,
name: user.name,
}, req.logger)
reply(userResponse).code(201)
}
示例6: handler
export default async function handler(req: Request, reply: IReply) {
const requestDoc: HackResource.TopLevelDocument = req.payload
const hack = new HackModel({
hackid: slugify(requestDoc.data.attributes.name),
name: requestDoc.data.attributes.name,
members: [],
})
try {
await hack.save()
} catch (err) {
if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
reply(Boom.conflict('Hack already exists'))
return
}
throw err
}
const hackResponse: HackResource.TopLevelDocument = {
links: {
self: `/hacks/${encodeURIComponent(hack.hackid)}`,
},
data: {
type: 'hacks',
id: hack.hackid,
attributes: {
name: hack.name,
},
},
}
const eventBroadcaster: EventBroadcaster = req.server.app.eventBroadcaster
eventBroadcaster.trigger('hacks_add', {
hackid: hack.hackid,
name: hack.name,
}, req.logger)
reply(hackResponse).code(201)
}
示例7: handler
export default async function handler(req: Request, reply: IReply) {
const requestDoc: TeamResource.TopLevelDocument = req.payload
const relationships = requestDoc.data.relationships
let members: JSONApi.ResourceIdentifierObject[] = []
let entries: JSONApi.ResourceIdentifierObject[] = []
if (relationships) {
if (relationships.members && relationships.members.data) {
if (!Array.isArray(relationships.members.data)) {
reply(Boom.badRequest())
return
}
members = relationships.members.data
}
if (relationships.entries && relationships.entries.data) {
if (!Array.isArray(relationships.entries.data)) {
reply(Boom.badRequest())
return
}
entries = relationships.entries.data
}
}
const team = new TeamModel({
teamid: slugify(requestDoc.data.attributes.name),
name: requestDoc.data.attributes.name,
motto: requestDoc.data.attributes.motto || null,
members: [],
entries: [],
})
let users: UserModel[] = []
let hacks: HackModel[] = []
if (members.length > 0) {
users = await UserModel.find({
userid: {
$in: members.map((member) => member.id.toString()),
},
}, '_id userid name').exec()
team.members = users.map((user) => user._id)
}
if (entries.length > 0) {
hacks = await HackModel.find({
hackid: {
$in: entries.map((entry) => entry.id.toString()),
},
}, '_id hackid name').exec()
team.entries = hacks.map((hack) => hack._id)
}
try {
await team.save()
} catch (err) {
if (err.code === MongoDBErrors.E11000_DUPLICATE_KEY) {
reply(Boom.conflict('Team already exists'))
return
}
throw err
}
const teamResponse: TeamResource.TopLevelDocument = {
links: {
self: `/teams/${encodeURIComponent(team.teamid)}`,
},
data: {
type: 'teams',
id: team.teamid,
attributes: {
name: team.name,
motto: team.motto,
},
relationships: {
members: {
links: { self: `/teams/${encodeURIComponent(team.teamid)}/members` },
data: users.map((user) => ({ type: 'users', id: user.userid })),
},
entries: {
links: { self: `/teams/${encodeURIComponent(team.teamid)}/entries` },
data: hacks.map((hack) => ({ type: 'hacks', id: hack.hackid })),
},
},
},
}
const eventBroadcaster: EventBroadcaster = req.server.app.eventBroadcaster
eventBroadcaster.trigger('teams_add', {
teamid: team.teamid,
name: team.name,
motto: team.motto,
members: users.map((user) => ({ userid: user.userid, name: user.name })),
entries: hacks.map((hack) => ({ hackid: hack.hackid, name: hack.name })),
}, req.logger)
reply(teamResponse).code(201)
}