Skip to content

Commit 2ada9a2

Browse files
committed
Allow AdonisJS configuration via Ace command (#115)
1 parent 1dc9b05 commit 2ada9a2

File tree

5 files changed

+71
-33
lines changed

5 files changed

+71
-33
lines changed

README.md

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -205,43 +205,26 @@ _Note:_ Apitally only works with H3 v2 and currently doesn't support nested apps
205205

206206
### AdonisJS
207207

208-
This is an example of how to use the Apitally middleware with a AdonisJS application.
209-
For further instructions, see our
210-
[setup guide for AdonisJS](https://docs.apitally.io/frameworks/adonisjs).
211-
212-
Create a configuration file at `config/apitally.ts`:
213-
214-
```javascript
215-
import { defineConfig } from "apitally/adonisjs";
208+
You can use the built-in Ace command to set up Apitally in your AdonisJS application:
216209

217-
const apitallyConfig = defineConfig({
218-
clientId: "your-client-id",
219-
env: "dev", // or "prod" etc.
220-
});
221-
222-
export default apitallyConfig;
210+
```bash
211+
node ace add apitally
223212
```
224213

225-
Register the Apitally provider in your `adonisrc.ts` file:
214+
This command will automatically:
226215

227-
```javascript
228-
export default defineConfig({
229-
// ... existing code ...
230-
providers: [
231-
// ... existing providers ...
232-
() => import("apitally/adonisjs/provider"),
233-
],
234-
});
235-
```
216+
- Create the configuration file at `config/apitally.ts`
217+
- Register the Apitally provider in `adonisrc.ts`
218+
- Add the Apitally middleware to your `start/kernel.ts` file
219+
- Add environment variables to `.env` and validation to `start/env.ts`
236220

237-
Register the Apitally middleware in your `start/kernel.ts` file:
221+
After running the command, you'll need to:
238222

239-
```javascript
240-
router.use([
241-
() => import("apitally/adonisjs/middleware"),
242-
// ... other middleware ...
243-
]);
244-
```
223+
- Set your `APITALLY_CLIENT_ID` in the `.env` file
224+
- Modify our exception handler in `app/exceptions/handler.ts` to capture validation and server errors in Apitally
225+
226+
For further instructions, see our
227+
[setup guide for AdonisJS](https://docs.apitally.io/frameworks/adonisjs).
245228

246229
## Getting help
247230

configure.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Allows users to add Apitally to their AdonisJS application using the built-in Ace command.
3+
*/
4+
5+
import type Configure from "@adonisjs/core/commands/configure";
6+
import { stubsRoot } from "./stubs/main.js";
7+
8+
export async function configure(command: Configure) {
9+
const codemods = await command.createCodemods();
10+
11+
await codemods.makeUsingStub(stubsRoot, "config/apitally.stub", {});
12+
13+
await codemods.registerMiddleware("router", [
14+
{
15+
path: "apitally/adonisjs/middleware",
16+
},
17+
]);
18+
19+
await codemods.updateRcFile((rcFile) => {
20+
rcFile.addProvider("apitally/adonisjs/provider");
21+
});
22+
23+
await codemods.defineEnvVariables({
24+
APITALLY_CLIENT_ID: "",
25+
APITALLY_ENV: "dev",
26+
});
27+
28+
await codemods.defineEnvValidations({
29+
leadingComment: "Variables for configuring the apitally package",
30+
variables: {
31+
APITALLY_CLIENT_ID: "Env.schema.string()",
32+
APITALLY_ENV: "Env.schema.string.optional()",
33+
},
34+
});
35+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
},
4343
"files": [
4444
"dist/",
45-
"README.md",
46-
"LICENSE"
45+
"stubs/",
46+
"configure.ts",
47+
"LICENSE",
48+
"README.md"
4749
],
4850
"exports": {
4951
"./adonisjs": {

stubs/config/apitally.stub

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{{
2+
exports({
3+
to: app.configPath('apitally.ts')
4+
})
5+
}}}
6+
import env from '#start/env'
7+
import app from '@adonisjs/core/services/app'
8+
import { defineConfig } from 'apitally/adonisjs'
9+
10+
const apitallyConfig = defineConfig({
11+
clientId: env.get('APITALLY_CLIENT_ID'),
12+
env: env.get('APITALLY_ENV', app.inProduction ? 'prod' : 'dev'),
13+
})
14+
15+
export default apitallyConfig

stubs/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { getDirname } from "@adonisjs/core/helpers";
2+
3+
export const stubsRoot = getDirname(import.meta.url);

0 commit comments

Comments
 (0)