Skip to content

Commit 3bf7765

Browse files
committed
feat: rewrite examples in typescript
1 parent 7dc6e02 commit 3bf7765

File tree

12 files changed

+491
-100
lines changed

12 files changed

+491
-100
lines changed

examples/auth.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1-
const AdminBro = require('admin-bro')
2-
const express = require('express')
3-
const mongoose = require('mongoose')
1+
import AdminBro from "admin-bro";
2+
import express from "express";
3+
import mongoose from "mongoose";
4+
import MongooseAdapter from "@admin-bro/mongoose";
45

5-
AdminBro.registerAdapter(require('@admin-bro/mongoose'))
6+
AdminBro.registerAdapter(MongooseAdapter);
67

7-
const AdminBroExpress = require('../index')
8+
import AdminBroExpress from "../index";
89

9-
// load the database models
10-
require('./mongoose/article-model')
11-
require('./mongoose/admin-model')
10+
import "./mongoose/article-model";
11+
import "./mongoose/admin-model";
1212

1313
const ADMIN = {
14-
15-
password: 'password',
16-
}
14+
15+
password: "password",
16+
};
1717

1818
const start = async () => {
19-
const connection = await mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost:27017/example')
20-
const app = express()
19+
const connection = await mongoose.connect(
20+
process.env.MONGO_URL || "mongodb://localhost:27017/example"
21+
);
22+
const app = express();
2123

2224
const adminBro = new AdminBro({
2325
databases: [connection],
24-
rootPath: '/admin',
25-
})
26+
rootPath: "/admin",
27+
});
2628

2729
const router = AdminBroExpress.buildAuthenticatedRouter(adminBro, {
2830
authenticate: async (email, password) => {
2931
if (ADMIN.password === password && ADMIN.email === email) {
30-
return ADMIN
32+
return ADMIN;
3133
}
32-
return null
34+
return null;
3335
},
34-
cookiePassword: 'somasd1nda0asssjsdhb21uy3g',
35-
})
36+
cookiePassword: "somasd1nda0asssjsdhb21uy3g",
37+
});
3638

37-
app.use(adminBro.options.rootPath, router)
39+
app.use(adminBro.options.rootPath, router);
3840

39-
app.listen(process.env.PORT || 8080, () => console.log('AdminBro is under localhost:8080/admin'))
40-
}
41+
app.listen(process.env.PORT || 8080, () =>
42+
console.log("AdminBro is under localhost:8080/admin")
43+
);
44+
};
4145

42-
start()
46+
start();

examples/mongoose/admin-model.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/mongoose/admin-model.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import mongoose from "mongoose";
2+
3+
const { Schema } = mongoose;
4+
5+
const AdminSchema = new Schema({
6+
email: String,
7+
password: String,
8+
});
9+
10+
const Admin = mongoose.model("Admin", AdminSchema);
11+
12+
module.exports = Admin;

examples/mongoose/article-model.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/mongoose/article-model.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import mongoose from "mongoose";
2+
3+
const { Schema } = mongoose;
4+
5+
const ArticleSchema = new Schema({
6+
title: String,
7+
content: String,
8+
author: String,
9+
createdAt: Date,
10+
published: Boolean,
11+
});
12+
13+
const Article = mongoose.model("Article", ArticleSchema);
14+
15+
module.exports = Article;

examples/simple.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/simple.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import AdminBro from "admin-bro";
2+
import express from "express";
3+
import mongoose from "mongoose";
4+
5+
import MongooseAdapter from "@admin-bro/mongoose";
6+
7+
AdminBro.registerAdapter(MongooseAdapter);
8+
9+
import AdminBroExpress from "../index";
10+
11+
import "./mongoose/article-model";
12+
import "./mongoose/admin-model";
13+
14+
const start = async () => {
15+
const connection = await mongoose.connect(
16+
process.env.MONGO_URL || "mongodb://localhost:27017/example"
17+
);
18+
const app = express();
19+
20+
const adminBro = new AdminBro({
21+
databases: [connection],
22+
rootPath: "/admin",
23+
});
24+
const router = AdminBroExpress.buildRouter(adminBro);
25+
26+
app.use(adminBro.options.rootPath, router);
27+
28+
app.listen(process.env.PORT || 8080, () =>
29+
console.log("AdminBro is under localhost:8080/admin")
30+
);
31+
};
32+
33+
start();

nodemon.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"verbose": false,
3+
"ignore": ["*.test.*", "*.spec.*"],
4+
"exec": "ts-node --files examples/simple.ts",
5+
"watch": [
6+
"src",
7+
"examples"
8+
]
9+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"dev": "rm -rf lib && tsc --watch",
88
"build": "rm -rf lib && tsc",
9-
"simple": "nodemon examples/simple.js",
9+
"simple": "nodemon",
1010
"test": "jest --config ./test/jest.json",
1111
"lint": "eslint './**/*.ts'",
1212
"check:all": "yarn lint && yarn build && yarn test",
@@ -65,13 +65,15 @@
6565
"husky": "^4.3.0",
6666
"jest": "^26.6.3",
6767
"mongoose": "^5.10.15",
68+
"nodemon": "^2.0.6",
6869
"prettier": "^2.2.0",
6970
"semantic-release": "^17.2.4",
7071
"semantic-release-jira-releases-sb": "^0.7.2",
7172
"semantic-release-slack-bot": "^1.6.2",
7273
"sinon": "^9.2.1",
7374
"sinon-chai": "^3.5.0",
7475
"ts-jest": "^26.4.4",
76+
"ts-node": "^9.0.0",
7577
"typescript": "^4.1.2"
7678
}
7779
}

src/authentication/logout.handler.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import AdminBro from "admin-bro";
2+
import { RequestHandler } from "express-serve-static-core";
3+
4+
export const createLogoutHandler = (admin: AdminBro): RequestHandler => async (
5+
request,
6+
response
7+
) => {
8+
request.session.destroy(() => {
9+
response.redirect(admin.options.loginPath);
10+
});
11+
};

0 commit comments

Comments
 (0)