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

Génère les types des payloads de l'API #722

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion confiture-rest-api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ lerna-debug.log*

.env

src/generated
src/generated

# Generated API typings
confiture-api.ts
4 changes: 3 additions & 1 deletion confiture-rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"lint": "eslint \"src/**/*.ts\" --fix",
"migrate:dev": "prisma migrate dev",
"migrate:prod": "prisma migrate deploy",
"postinstall": "prisma generate"
"postinstall": "prisma generate && yarn generate-api-types",
"generate-api-types": "rimraf dist && GENERATE_TYPES=1 nest start --entryFile generate-api-typings.js"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.218.0",
Expand Down Expand Up @@ -59,6 +60,7 @@
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.0.1",
"openapi-typescript": "^6.7.6",
"prettier": "^3.1.1",
"source-map-support": "^0.5.20",
"ts-loader": "^9.2.3",
Expand Down
4 changes: 3 additions & 1 deletion confiture-rest-api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { UserMiddleware } from "./auth/user.middleware";
imports: [
ConfigModule.forRoot({
isGlobal: true,
validationSchema: configValidationSchema
validationSchema: !process.env.GENERATE_TYPES
? configValidationSchema
: undefined
}),
FeedbackModule,
AuditsModule,
Expand Down
36 changes: 18 additions & 18 deletions confiture-rest-api/src/audits/dto/audit-report.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,7 @@ export class AuditReportDto {
*/
accessibilityRate: number;

/**
* @example {
* total: 106;
* compliant: 30;
* notCompliant: 46;
* blocking: 12;
* applicable: 76;
* notApplicable: 30;
* }
*/
criteriaCount: {
total: number;
compliant: number;
notCompliant: number;
blocking: number;
applicable: number;
notApplicable: number;
};
criteriaCount: CriteriaCount;

/** Global distribution of criteria by result */
resultDistribution: ResultDistribution;
Expand All @@ -67,6 +50,21 @@ export class AuditReportDto {
results: ReportCriterionResult[];
}

class CriteriaCount {
/** @example 106 */
total: number;
/** @example 30 */
compliant: number;
/** @example 46 */
notCompliant: number;
/** @example 12 */
blocking: number;
/** @example 76 */
applicable: number;
/** @example 30 */
notApplicable: number;
}

class RawAndPercentage {
/**
* @example 47
Expand Down Expand Up @@ -204,6 +202,8 @@ class ReportCriterionResult {
recommandation: string | null;

notApplicableComment: string | null;

quickWin: boolean;
}

class ExampleImage {
Expand Down
24 changes: 24 additions & 0 deletions confiture-rest-api/src/generate-api-typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { writeFile } from "fs/promises";
import openapiTS, { OpenAPI3 } from "openapi-typescript";
import { resolve } from "path";

import { AppModule } from "./app.module";

async function main() {
const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder().setTitle("Confiture API").build();
const document = SwaggerModule.createDocument(app, config);

const ast = await openapiTS(document as OpenAPI3);
const fileContent = "/* eslint-disable */\n" + ast;
const resolvedPath = resolve(process.cwd(), "./confiture-api.ts");
await writeFile(resolvedPath, fileContent, {
encoding: "utf-8"
});
console.log("✅ Typings saved to", resolvedPath);
}

main();
9 changes: 8 additions & 1 deletion confiture-rest-api/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "scripts"]
"exclude": [
"node_modules",
"test",
"dist",
"**/*spec.ts",
"scripts",
"confiture-api.ts"
]
}
1 change: 1 addition & 0 deletions confiture-web-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage
accessibilite.numerique.gouv.fr
src/methodologies.json
src/criteres.json
src/types/confiture-api.ts
6 changes: 3 additions & 3 deletions confiture-web-app/src/components/report/ReportErrors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ const defaultUserImpactFillters = [
null
];

const userImpactFilters = ref<Array<CriterionResultUserImpact | null>>(
defaultUserImpactFillters
);
const userImpactFilters = ref<
Array<CriterionResultUserImpact | `${CriterionResultUserImpact}` | null>
>(defaultUserImpactFillters);

const disabledResetFilters = computed(
() =>
Expand Down
113 changes: 3 additions & 110 deletions confiture-web-app/src/types/report.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,4 @@
import { AuditType, CriteriumResult } from "../types";
import { paths } from "./confiture-api";

export interface AuditReport {
consultUniqueId: string;

contactEmail?: string;
contactFormUrl?: string;

procedureInitiator?: string;
procedureName: string;
procedureUrl?: string;

creationDate?: string;
publishDate?: string;
updateDate?: string;

notCompliantContent?: string;
derogatedContent?: string;
notInScopeContent?: string;
notes?: string;

auditType: AuditType;

context: AuditReportContext;

accessibilityRate: number;

criteriaCount: {
total: number;
compliant: number;
notCompliant: number;
blocking: number;
applicable: number;
notApplicable: number;
};

/** Global distribution of criteria by result */
resultDistribution: ResultDistribution;

/** Distribution of criteria by page */
pageDistributions: PageResultDistribution[];

/** Distribution of criteria by topic */
topicDistributions: TopicResultDistribution[];

results: Array<
Omit<CriteriumResult, "exampleImages"> & {
exampleImages: {
url: string;
filename: string;
}[];
}
>;
}

interface ResultDistribution {
compliant: {
raw: number;
percentage: number;
};
notCompliant: {
raw: number;
percentage: number;
};
notApplicable: {
raw: number;
percentage: number;
};
}

interface PageResultDistribution extends ResultDistribution {
name: string;
}

interface TopicResultDistribution extends ResultDistribution {
name: string;
}

interface AuditReportContext {
referencial: string;

auditorName: string;
auditorEmail: string | null;
auditorOrganisation: string;

technologies: string[];

samples: PageSample[];

tools: string[];

desktopEnvironments: Environment[];
mobileEnvironments: Environment[];
}

interface PageSample {
// number: number;
id: number;
order: number;
name: string;
url: string;
}

interface Environment {
operatingSystem: string;
operatingSystemVersion?: string;
assistiveTechnology: string;
assistiveTechnologyVersion?: string;
browser: string;
browserVersion?: string;
}
export type AuditReport =
paths["/reports/{consultUniqueId}"]["get"]["responses"]["200"]["content"]["application/json"];
2 changes: 2 additions & 0 deletions confiture-web-app/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export enum AuditType {
FULL = "FULL"
}

export type AuditTypeString = `${AuditType}`;

export enum AuditStatus {
IN_PROGRESS = "IN_PROGRESS",
COMPLETED = "COMPLETED",
Expand Down
9 changes: 6 additions & 3 deletions confiture-web-app/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AuditReport,
AuditStatus,
AuditType,
AuditTypeString,
CriterionResultUserImpact,
CriteriumResultStatus
} from "./types";
Expand Down Expand Up @@ -44,7 +45,7 @@ const FORMATTED_USER_IMPACT = {
* Format a criterion result user impact type string into French.
*/
export function formatUserImpact(
userImpact: CriterionResultUserImpact
userImpact: CriterionResultUserImpact | `${CriterionResultUserImpact}`
): string {
return FORMATTED_USER_IMPACT[userImpact];
}
Expand All @@ -59,7 +60,9 @@ const FORMATTED_STATUS = {
/**
* Format a criterion result status type string into French.
*/
export function formatStatus(status: CriteriumResultStatus): string {
export function formatStatus(
status: CriteriumResultStatus | `${CriteriumResultStatus}`
): string {
return FORMATTED_STATUS[status];
}

Expand All @@ -72,7 +75,7 @@ const CRITERIA_COUNT = {
/**
* Return the number of criteria for a given audit type.
*/
export function getCriteriaCount(auditType: AuditType): number {
export function getCriteriaCount(auditType: AuditTypeString): number {
return CRITERIA_COUNT[auditType];
}

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"cypress": "^13.6.1",
"husky": "^8.0.3",
"lint-staged": "^15.2.0"
},
"scripts": {
"copytypes": "yarn workspace confiture-rest-api run generate-api-types && cp ./confiture-rest-api/confiture-api.ts ./confiture-web-app/src/types",
"postinstall": "yarn copytypes"
}
}
Loading