Skip to content

Commit

Permalink
chore: update to latest Remult version #43 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaelih authored Apr 12, 2024
1 parent 8a33805 commit 590f256
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 35 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ You can use Docker's standalone service and run it locally or on your machine.
Create a `.env` file with the following content:
```dotenv
JWT_SECRET=something-difficult-to-break
# Enable /api/admin route for admin interface
ADMIN_UI=true
```

Run it locally or wherever:
Expand Down
3 changes: 2 additions & 1 deletion apps/api/example.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PORT=9000
HOST=0.0.0.0
JWT_SECRET=something-difficult-to-break
JWT_SECRET=something-difficult-to-break
ADMIN_UI=true
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"mongodb": "^6.3.0",
"pg": "^8.11.3",
"redis": "^4.6.12",
"remult": "^0.24.1",
"remult": "^0.25.7",
"tsx": "^4.6.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions apps/api/services/remult-api-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RemultApiProviderService {
dataProvider: await getDataProvider(),
entities: entitiesList,
controllers,
admin: process.env.ADMIN_UI === 'true',
async getUser(req) {
return req.user || { id: '', tenant: defaultTenant };
}
Expand Down
54 changes: 54 additions & 0 deletions apps/test-tsx-app/admin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test, { after, describe } from 'node:test';
import { getPriciSdk } from './utils/prici-runner.js';
import assert from 'node:assert';

const authToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJpYXQiOjE1MTYyMzkwMjJ9.e4vybEZSMruiGm8XgTxlD2QDGuOBz5l3gRfmjlmb1PY';

describe('admin api route while ADMIN_UI=false', async () => {
const { quit, priciUBaseUrl } = await getPriciSdk();

after(() => {
quit();
});

test('should throw 401 exception when no auth token is sent', async () => {
const response = await fetch(`${priciUBaseUrl}/api/admin`);

assert.deepStrictEqual(response.status, 401);
});

test('should throw 404 exception when user is authenticated', async () => {
const response = await fetch(`${priciUBaseUrl}/api/admin`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});

assert.deepStrictEqual(response.status, 404);
});
});

describe('admin api route while ADMIN_UI=true', async () => {
const { quit, priciUBaseUrl } = await getPriciSdk({ adminUi: true });

after(() => {
quit();
});

test('should throw 401 exception when user is not authenticated', async () => {
const response = await fetch(`${priciUBaseUrl}/api/admin`);

assert.deepStrictEqual(response.status, 401);
});

test('should return 200 when user is authenticated', async () => {
const response = await fetch(`${priciUBaseUrl}/api/admin`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});

assert.deepStrictEqual(response.status, 200);
});
});
16 changes: 11 additions & 5 deletions apps/test-tsx-app/utils/prici-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { setTimeout } from 'node:timers/promises';
import jsonwebtoken from 'jsonwebtoken';
import { initialize } from '@prici/sdk';

interface priciOptions {
adminUi?: boolean;
}

const token = 'abcd';
const tenantName = 'demo-tenant-' + Math.random();


async function runPriciInstance() {
async function runPriciInstance({ adminUi }: priciOptions) {
let RANDOM_PORT = Math.floor(Math.random() * 10000).toString()

if (RANDOM_PORT.length < 4) {
Expand All @@ -19,6 +22,7 @@ async function runPriciInstance() {
...process.env,
PORT: RANDOM_PORT,
JWT_SECRET: token,
ADMIN_UI: adminUi ? 'true' : undefined,
};

const server = $`node --import tsx --watch server.ts`;
Expand All @@ -35,16 +39,18 @@ async function runPriciInstance() {
}


export async function getPriciSdk() {
const server = await runPriciInstance();
export async function getPriciSdk(options: priciOptions = {}) {
const server = await runPriciInstance(options);
const priciUBaseUrl = 'http://localhost:' + server.port;

return {
sdk: initialize({
token: jsonwebtoken.sign({
tenant: tenantName,
}, token),
priciUBaseUrl: 'http://localhost:' + server.port,
priciUBaseUrl,
}),
quit: () => server.quit(),
priciUBaseUrl,
};
}
2 changes: 2 additions & 0 deletions documentation/content/introduction/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ It will also do a lot more, but our main context is to help the developers to de
Create a `.env` file with the following content:
```dotenv
JWT_SECRET=something-difficult-to-break
# Enable /api/admin route for admin interface
ADMIN_UI=true
```

Run it locally or wherever:
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "MIT",
"dependencies": {
"@prici/shared-remult": "workspace:^",
"remult": "^0.24.1"
"remult": "^0.25.7"
},
"peerDependencies": {
"@nestjs/common": "^9.0.0 || ^10.0.0"
Expand Down
2 changes: 1 addition & 1 deletion libs/shared-remult/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"remult": "^0.24.1"
"remult": "^0.25.7"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit 590f256

Please sign in to comment.