parent
0764085ef9
commit
0e5a0a5a5c
@ -0,0 +1,25 @@
|
||||
import BaseApiController from "../../../lib/src/http/ApiController.ts";
|
||||
import {Request} from "../../../lib/src/http/Request.ts";
|
||||
import {redirect} from "../../../lib/src/http/response/helpers.ts";
|
||||
|
||||
export default class ApiController extends BaseApiController {
|
||||
|
||||
get_good(request: Request) {
|
||||
return {
|
||||
session_key: request.session.get_key(),
|
||||
}
|
||||
}
|
||||
|
||||
get_arr(request: Request) {
|
||||
return ['alpha', 'bravo', 'charlie', 'delta', 'echo']
|
||||
}
|
||||
|
||||
get_msg(request: Request) {
|
||||
return 'Hello, world!'
|
||||
}
|
||||
|
||||
get_redirect(request: Request) {
|
||||
return redirect('/home')
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
import Controller from "./Controller.ts";
|
||||
import {Request} from "./Request.ts";
|
||||
import ResponseFactory from "./response/ResponseFactory.ts";
|
||||
import * as api from '../support/api.ts'
|
||||
import JSONResponseFactory from "./response/JSONResponseFactory.ts";
|
||||
|
||||
export default class ApiController extends Controller {
|
||||
public get_bound_method(method_name: string): (...args: any[]) => any {
|
||||
// @ts-ignore
|
||||
if ( typeof this[method_name] !== 'function' ) {
|
||||
throw new TypeError(`Attempt to get bound method for non-function type: ${method_name}`)
|
||||
}
|
||||
|
||||
return async (...args: any[]): Promise<any> => {
|
||||
if ( args.length !== 1 ) {
|
||||
// @ts-ignore
|
||||
return this[method_name](...args)
|
||||
}
|
||||
|
||||
const request: Request = args[0]
|
||||
// @ts-ignore
|
||||
const result = await this[method_name](request)
|
||||
|
||||
// If we have a request or a response factory, return that
|
||||
if ( result instanceof Request || result instanceof ResponseFactory ) {
|
||||
return result
|
||||
}
|
||||
|
||||
console.log('result', result)
|
||||
|
||||
// Otherwise, try to package the results as an API response
|
||||
if ( Array.isArray(result) ) {
|
||||
return this.make(JSONResponseFactory, api.many(result))
|
||||
} else if ( typeof result === 'string' ) {
|
||||
return this.make(JSONResponseFactory, api.message(result))
|
||||
} else {
|
||||
return this.make(JSONResponseFactory, api.one(result))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
export interface APIResponse {
|
||||
success: boolean,
|
||||
message?: string,
|
||||
data?: any,
|
||||
error?: {
|
||||
name: string,
|
||||
message: string,
|
||||
stack?: string[],
|
||||
}
|
||||
}
|
||||
|
||||
export function message(message: string): APIResponse {
|
||||
return {
|
||||
success: true,
|
||||
message,
|
||||
}
|
||||
}
|
||||
|
||||
export function one(record: any): APIResponse {
|
||||
return {
|
||||
success: true,
|
||||
data: record,
|
||||
}
|
||||
}
|
||||
|
||||
export function many(records: any[]): APIResponse {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
records,
|
||||
total: records.length,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function error(error: string | Error): APIResponse {
|
||||
if ( typeof error === 'string' ) {
|
||||
return {
|
||||
success: false,
|
||||
message: error,
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
message: error.message,
|
||||
error: {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack ? error.stack.split(/\s+at\s+/).slice(1) : [],
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue