Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable typescript strictNullChecks #468

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions prisma/seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export class Seeder {
async generateLog(): Promise<void> {
const packageVersion = await getInstalledPackageVersion('idn-area-data');

if (!packageVersion) {
throw new Error(
'idn-area-data package is not installed. Make sure to run `pnpm install` first.',
);
}

await this.prisma.seederLogs.create({
data: { dataVersion: packageVersion },
});
Expand Down
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { VillageModule } from './village/village.module';
inject: [ConfigService],
useFactory: (config: ConfigService) => [
{
ttl: seconds(config.get('APP_THROTTLE_TTL')),
limit: config.get('APP_THROTTLE_LIMIT'),
ttl: seconds(config.get('APP_THROTTLE_TTL') as number),
limit: config.get('APP_THROTTLE_LIMIT') as number,
skipIf: () => config.get('APP_ENABLE_THROTTLE') !== 'true',
},
],
Expand Down
12 changes: 7 additions & 5 deletions src/common/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export type AppConfig = {
export const appConfig: AppConfig = {
env: (process.env.APP_ENV as AppConfig['env']) || 'dev',
host: process.env.APP_HOST || '0.0.0.0',
port: Number.parseInt(process.env.APP_PORT) || 3000,
port: Number.parseInt(process.env.APP_PORT || '3000'),
pagination: {
maxPageSize:
Number.parseInt(process.env.APP_PAGINATION_MAX_PAGE_SIZE) || 100,
defaultPageSize:
Number.parseInt(process.env.APP_PAGINATION_DEFAULT_PAGE_SIZE) || 10,
maxPageSize: Number.parseInt(
process.env.APP_PAGINATION_MAX_PAGE_SIZE || '100',
),
defaultPageSize: Number.parseInt(
process.env.APP_PAGINATION_DEFAULT_PAGE_SIZE || '10',
),
},
} as const;
6 changes: 3 additions & 3 deletions src/common/decorator/EqualsAny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export function EqualsAny(
@ValidatorConstraint({ name: 'equalsAny' })
export class EqualsAnyConstraint implements ValidatorConstraintInterface {
validate(value: any, args?: ValidationArguments): boolean {
const [validValues] = args.constraints as [string[], any];
const [validValues] = (args?.constraints ?? []) as [string[], any];
return typeof value === 'string' && validValues.includes(value);
}

defaultMessage(validationArguments?: ValidationArguments): string {
const { constraints, property } = validationArguments;
const { constraints, property } = validationArguments ?? {};
return `${property} must equals one of these: ${(
constraints[0] as string[]
(constraints?.[0] as string[] | undefined) ?? []
).join(', ')}.`;
}
}
4 changes: 2 additions & 2 deletions src/common/decorator/IsNotSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function IsNotSymbol(
@ValidatorConstraint({ name: 'isNotSymbol' })
export class IsNotSymbolConstraint implements ValidatorConstraintInterface {
validate(value: any, args?: ValidationArguments): boolean {
const [allowedSymbols = ''] = args.constraints as [string, any];
const [allowedSymbols = ''] = (args?.constraints ?? []) as [string, any];
const safeAllowedSymbols = allowedSymbols
.split('')
.map((s) => `\\${s}`)
Expand All @@ -42,7 +42,7 @@ export class IsNotSymbolConstraint implements ValidatorConstraintInterface {
}

defaultMessage(validationArguments?: ValidationArguments): string {
const { constraints, property } = validationArguments;
const { constraints, property } = validationArguments ?? {};
const [allowedSymbols = ''] = constraints as [string, any];

return allowedSymbols
Expand Down
4 changes: 1 addition & 3 deletions src/common/decorator/api-data-response.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export const ApiDataResponse = <Model extends Type<any>>(
type: 'object',
nullable: options.multiple ? undefined : true,
properties: {
total: options.multiple
? { type: 'number', example: 1 }
: undefined,
total: options.multiple ? { type: 'number', example: 1 } : {},
},
},
},
Expand Down
14 changes: 9 additions & 5 deletions src/common/utils/coordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ export const convertCoordinate = (coordinate: string): number[] => {
throw new Error('Invalid coordinate format');
}

const [a, b, c, d, e, f] = coordinate.match(/[0-9,\.]+/g);
const [y, x] = coordinate.match(/(N|S|E|W)/g);
const degrees = coordinate.match(/[0-9,\.]+/g);
const polars = coordinate.match(/(N|S|E|W)/g);

const latitude = calculate(a, b, c, y);
const longitude = calculate(d, e, f, x);
if (!degrees || !polars) {
throw new Error('Invalid coordinate format');
}

const [a, b, c, d, e, f] = degrees;
const [y, x] = polars;

return [latitude, longitude];
return [calculate(a, b, c, y), calculate(d, e, f, x)]; // [latitude, longitude]
};
4 changes: 4 additions & 0 deletions src/common/utils/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ export const validateDBConfig = (...vars: (keyof typeof dbConfig)[]) => {
* Get the database provider features, based on the current config.
*/
export const getDBProviderFeatures = (): DBProviderFeatures | undefined => {
if (!dbConfig.provider) {
return undefined;
}

return dbProviderConfig[dbConfig.provider]?.features;
};
4 changes: 2 additions & 2 deletions src/district/district.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ describe('DistrictService', () => {
const testCode = '110101';
const expectedDistrict = districts.find((d) => d.code === testCode);
const expectedRegency = regencies.find(
(r) => r.code === expectedDistrict.regencyCode,
(r) => r.code === expectedDistrict?.regencyCode,
);
const expectedProvince = provinces.find(
(p) => p.code === expectedRegency.provinceCode,
(p) => p.code === expectedRegency?.provinceCode,
);

const findUniqueSpy = vitest
Expand Down
10 changes: 5 additions & 5 deletions src/island/island.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ describe('IslandService', () => {
describe('findByCode', () => {
it('should return an island', async () => {
const testCode = '110140001';
const expectedIsland = islands.find((i) => i.code === testCode);
const expectedIsland = islands.find((i) => i.code === testCode) as Island;
const expectedRegency = regencies.find(
(r) => r.code === expectedIsland.regencyCode,
(r) => r.code === expectedIsland?.regencyCode,
);
const expectedProvince = provinces.find(
(p) => p.code === expectedRegency.provinceCode,
(p) => p.code === expectedRegency?.provinceCode,
);

const findUniqueSpy = vitest
Expand Down Expand Up @@ -259,10 +259,10 @@ describe('IslandService', () => {

it('should return an island without regency', async () => {
const testCode = '120040001';
const expectedIsland = islands.find((i) => i.code === testCode);
const expectedIsland = islands.find((i) => i.code === testCode) as Island;
const expectedProvince = provinces.find(
(p) => p.code === testCode.slice(0, 2),
);
) as Province;

const findUniqueIslandSpy = vitest
.spyOn(prismaService.island, 'findUnique')
Expand Down
4 changes: 2 additions & 2 deletions src/island/island.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ export class IslandService {
...this.addDecimalCoordinate(island),
parent: {
regency: null,
province: await this.prisma.province.findUnique({
province: (await this.prisma.province.findUnique({
where: {
code: code.slice(0, 2),
},
}),
})) as NonNullable<IslandWithParent['parent']['province']>,
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function bootstrap() {
// Init API documentation with Swagger.
const docConfig = new DocumentBuilder()
.setTitle('Indonesia Area API')
.setVersion(process.env.npm_package_version)
.setVersion(process.env.npm_package_version as string)
.setDescription(
'API that provides information about Indonesia administrative area.',
)
Expand Down
3 changes: 2 additions & 1 deletion src/province/province.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ describe('ProvinceService', () => {
describe('findByCode', () => {
it('should return a province when given a valid code', async () => {
const testCode = '11';
const expectedProvince = provinces.find((p) => p.code === testCode);
const expectedProvince =
provinces.find((p) => p.code === testCode) ?? null;

const findUniqueSpy = vitest
.spyOn(prismaService.province, 'findUnique')
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"strictNullChecks": true,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
Expand Down
Loading