@stamhoofd/redirecter 2.119.0 → 2.120.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- import { DecodedRequest, Endpoint, Request, Response } from '@simonbackx/simple-endpoints';
2
- import { Country } from '@stamhoofd/structures';
3
-
1
+ import type { DecodedRequest, Request} from '@simonbackx/simple-endpoints';
2
+ import { Endpoint, Response } from '@simonbackx/simple-endpoints';
3
+ import { Country } from '@stamhoofd/types/Country';
4
4
  import { Geolocator } from '../classes/Geolocator.js';
5
5
 
6
6
  type Params = Record<string, never>;
@@ -8,25 +8,13 @@ type Body = undefined;
8
8
  type Query = undefined;
9
9
  type ResponseBody = string;
10
10
 
11
- function getRequestIP(request: Request): string {
12
- let ipAddress = request.request?.socket.remoteAddress;
13
- if (request.headers['x-real-ip'] && typeof request.headers['x-real-ip'] === 'string' && (ipAddress === '127.0.0.1' || ipAddress === '0.0.0.0')) {
14
- ipAddress = request.headers['x-real-ip'];
15
- }
16
- if (!ipAddress) {
17
- ipAddress = '?';
18
- }
19
-
20
- return ipAddress.split(':', 2)[0];
21
- }
22
-
23
11
  export class RedirectEndpoint extends Endpoint<Params, Query, Body, ResponseBody> {
24
12
  protected doesMatch(_request: Request): [true, Params] | [false] {
25
13
  return [true, {}];
26
14
  }
27
15
 
28
16
  async handle(request: DecodedRequest<Params, Query, Body>) {
29
- const ip = getRequestIP(request.request);
17
+ const ip = request.request.getIP()
30
18
 
31
19
  const country = Geolocator.shared.getCountry(ip);
32
20
 
@@ -5,7 +5,7 @@ backendEnv.load({ service: 'redirecter' }).catch((error) => {
5
5
  console.error('Failed to load environment:', error);
6
6
  process.exit(1);
7
7
  }).then(async () => {
8
- await import('./src/boot.js');
8
+ await import('./boot.js');
9
9
  }).catch((error) => {
10
10
  console.error('Failed to start the API:', error);
11
11
  process.exit(1);
package/stamhoofd.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import '../../../environment.d.ts';
2
+
1
3
  export {};
2
4
 
3
5
  /**
@@ -0,0 +1,20 @@
1
+ // first import nock
2
+ import nock from 'nock';
3
+
4
+ // prevent nock import from being removed on save
5
+ console.log('Imported nock: ', !!nock);
6
+
7
+ import { TestUtils } from '@stamhoofd/test-utils';
8
+
9
+
10
+ // Set timezone!
11
+ process.env.TZ = 'UTC';
12
+
13
+ // Quick check
14
+ if (new Date().getTimezoneOffset() !== 0) {
15
+ throw new Error('Process should always run in UTC timezone');
16
+ }
17
+
18
+ export async function setup() {
19
+ TestUtils.globalSetup();
20
+ };
@@ -0,0 +1,30 @@
1
+ // first import nock
2
+
3
+ import { Column } from '@simonbackx/simple-database';
4
+ import { Request } from '@simonbackx/simple-endpoints';
5
+ import { Version } from '@stamhoofd/structures';
6
+ import { TestUtils } from '@stamhoofd/test-utils';
7
+
8
+ Error.stackTraceLimit = 100;
9
+
10
+ // Set version of saved structures
11
+ Column.setJSONVersion(Version);
12
+
13
+ // Automatically set endpoint default version to latest one (only in tests!)
14
+ Request.defaultVersion = Version;
15
+
16
+ // Set timezone!
17
+ process.env.TZ = 'UTC';
18
+
19
+ // Quick check
20
+ if (new Date().getTimezoneOffset() !== 0) {
21
+ throw new Error('Process should always run in UTC timezone');
22
+ }
23
+
24
+ console.log = () => {};
25
+
26
+ beforeAll(async () => {
27
+ // Override default $t handlers
28
+ TestUtils.loadEnvironment();
29
+ });
30
+ TestUtils.setup();
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": [
8
+ "./src",
9
+ "./stamhoofd.d.ts"
10
+ ],
11
+ "exclude": [
12
+ "./src/**/*.spec.ts",
13
+ "./src/**/*.test.ts"
14
+ ]
15
+ }
package/tsconfig.json CHANGED
@@ -1,36 +1,12 @@
1
1
  {
2
- "compilerOptions": {
3
- "target": "es2022", // needs to be es2019 to support optional chaining. Node.js doesn't support optional chaining yet, so we need the transpiling
4
- "module": "commonjs",
5
- "jsx": "preserve",
6
- "importHelpers": true,
7
- "moduleResolution": "node",
8
- "experimentalDecorators": true,
9
- "esModuleInterop": true,
10
- "allowSyntheticDefaultImports": true,
11
- "sourceMap": true,
12
- "strictNullChecks": true,
13
- "declaration": true,
14
- "declarationMap": true,
15
- "outDir": "dist",
16
- "lib": [
17
- "es2022"
18
- ],
19
- "types": [
20
- "node",
21
- "jest",
22
- "@stamhoofd/backend-i18n",
23
- ]
24
- },
25
- "include": [
26
- "src/**/*.ts",
27
- "tests/**/*.ts",
28
- "index.ts",
29
- "migrations.ts",
30
- "../../../*.d.ts",
31
- "*.d.ts"
32
- ],
33
- "exclude": [
34
- "node_modules"
2
+ "extends": "../../../tsconfig.base.json",
3
+ "files": [],
4
+ "references": [
5
+ {
6
+ "path": "./tsconfig.build.json"
7
+ },
8
+ {
9
+ "path": "./tsconfig.test.json"
10
+ }
35
11
  ]
36
12
  }
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../../../tsconfig.test.json",
3
+ "compilerOptions": {
4
+ "rootDir": ".",
5
+ "outDir": "dist"
6
+ },
7
+ "references": [
8
+ { "path": "./tsconfig.build.json" }
9
+ ],
10
+ "include": [
11
+ "./stamhoofd.d.ts",
12
+ "../../../jest-extended.d.ts",
13
+ "./src/**/*.spec.ts",
14
+ "./src/**/*.test.ts",
15
+ "./tests"
16
+ ]
17
+ }
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globalSetup: './tests/vitest.global.setup.ts',
6
+ setupFiles: ['./tests/vitest.setup.ts'],
7
+ watch: false,
8
+ globals: true,
9
+ root: import.meta.dirname,
10
+ isolate: true,
11
+ maxWorkers: 1, // For now we can't run parallel because all test files use the same database
12
+ },
13
+ });
package/eslint.config.mjs DELETED
@@ -1,5 +0,0 @@
1
- import stamhoofdEslint from 'eslint-plugin-stamhoofd';
2
-
3
- export default [
4
- ...stamhoofdEslint.configs.backend,
5
- ];