Last active
June 25, 2022 09:48
-
-
Save kwbtdisk/7a11003ec4bc3cbe3359e92381bdff1a to your computer and use it in GitHub Desktop.
CORE API Endpoint を追加する方法 https://youtu.be/5Baq9BR7Ku4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api-directus/extensions/endpoints/samplepath/index.js | |
const {$serverErrorReporter} = require("../../../../CORE/src/server/$serverErrorReporter"); | |
module.exports = function ( | |
router, | |
directusOptionalArgs, | |
) { | |
// 別のTypescript ファイルへ分ける場合 | |
// require('../../../../models/modelServices/testApiEndpoint').registerTestApiEndpoints(router, directusOptionalArgs) | |
// 最小限の例 [GET] `/samplepath/minimal-sample` | |
router.get('/minimal-sample', async (req, res, next) => { | |
res.json({some: 'somesome'}) | |
}) | |
// [GET] `/samplepath/new-api-path` | |
// CORE Front から Callする方法: $core.$d.transport.get('/samplepath/new-api-path', { | |
// params: { | |
// some: 1234, | |
// other: {name: 'me', anotherProp: 'wwwww'}, | |
// another: true | |
// } | |
// }) | |
router.get('/new-api-path', async (req, res, next) => { | |
try { | |
// トークンをチェック (非ログインユーザにもアクセスさせて良い場合には、下記行をコメントアウト可) | |
verifyTokenFromRequest(req) | |
// クエリパラメータを取得 | |
const {some, other, another} = req.query | |
// await .... | |
// データ取得をする例 | |
const {ItemsService} = directusOptionalArgs.services; | |
const {ServiceUnavailableException} = directusOptionalArgs.exceptions; | |
const selectOptionsMasterItemService = new ItemsService('selectOptionsMaster', { | |
schema: req.schema, | |
// リクエストユーザの権限を判別させたい場合は、req.accountability をセット。特権権限にしたい場合はnull | |
accountability: null // req.accountability | |
}); | |
const s = '' | |
const records = await selectOptionsMasterItemService.readByQuery({fields: ['*'], limit: 10}) | |
// レスポンスデータ整形 | |
const response = { | |
joined: `${JSON.stringify({some, other, another})}`, | |
anotherNumber: 100, | |
records | |
} | |
// レスポンスデータ整形 | |
res.json(response) | |
} catch (e) { | |
console.error(e) | |
// sentryへエラーを送信 | |
$serverErrorReporter.report(e) | |
// 500エラーを返す | |
res.status(500).send(e.message) | |
} | |
}) | |
// [POST] `/samplepath/new-api-path` | |
router.post('/new-api-path', async (req, res, next) => { | |
try { | |
// トークンをチェック (非ログインユーザにもアクセスさせて良い場合には、下記行をコメントアウト可) | |
verifyTokenFromRequest(req) | |
// クエリパラメータを取得 | |
const {some, other, another} = req.query | |
// bodyを取得 | |
const {imageUrl} = req.body | |
// await .... | |
const response = { | |
joined: `${JSON.stringify({some, other, another})}`, | |
anotherNumber: 100, | |
body: req.body | |
} | |
res.send(response) | |
} catch (e) { | |
console.error(e) | |
$serverErrorReporter.report(e) | |
res.status(500).send(e.message) | |
} | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// models/modelServices/testApiEndpoint.ts | |
import { getItemsServiceByModelName } from '../../CORE/src/server/ItemsServiceByModelName' | |
import { verifyTokenFromRequest } from '../../CORE/src/server/utils' | |
export const registerTestApiEndpointSomeFunc = (router, directusOptionalArgs) => { | |
router.get('/somePath', async (req, res, next) => { | |
try { | |
verifyTokenFromRequest(req) | |
const usersItemService = await getItemsServiceByModelName('directus_users') | |
const users = await usersItemService.readByQuery({fields: ['*'], limit: 10}) | |
const response = { | |
message: 'Hello World!', | |
users | |
} | |
// レスポンスを返す | |
res.json(response) | |
} catch (e) { | |
} | |
}) | |
} | |
class SomeComplexProcesses { | |
constructor({arg1, arg2}) { | |
} | |
async exec() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment