From e0d0d98562e5205543fab9d12cfc9e40c8ff9a7a Mon Sep 17 00:00:00 2001 From: Douglas Duteil Date: Mon, 23 Dec 2024 16:49:28 +0100 Subject: [PATCH] refactor(insee): extract insee connector to individual pkg (#896) --- .changeset/hip-houses-confess.md | 7 +++ Dockerfile | 6 +- package-lock.json | 29 ++++++++- package.json | 4 +- packages/insee/package.json | 63 +++++++++++++++++++ .../src/token/get-insee-access-token.test.ts | 26 ++++++++ .../insee/src/token/get-insee-access-token.ts | 42 +++++++++++++ packages/insee/src/token/index.ts | 3 + packages/insee/tsconfig.json | 18 ++++++ packages/insee/tsconfig.lib.json | 9 +++ 10 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 .changeset/hip-houses-confess.md create mode 100644 packages/insee/package.json create mode 100644 packages/insee/src/token/get-insee-access-token.test.ts create mode 100644 packages/insee/src/token/get-insee-access-token.ts create mode 100644 packages/insee/src/token/index.ts create mode 100644 packages/insee/tsconfig.json create mode 100644 packages/insee/tsconfig.lib.json diff --git a/.changeset/hip-houses-confess.md b/.changeset/hip-houses-confess.md new file mode 100644 index 00000000..2719a987 --- /dev/null +++ b/.changeset/hip-houses-confess.md @@ -0,0 +1,7 @@ +--- +"@gouvfr-lasuite/proconnect.insee": minor +--- + +♻️ Prélevement d'un partie du connecteur INSEE + +Dans le cadres la migration du script d'import de comptes coop, une partie de l'API INSEE est déplacées dans le package `@gouvfr-lasuite/proconnect.insee` pour permettre leur réutilisation dans Hyyypertool. diff --git a/Dockerfile b/Dockerfile index 192efafe..de2ffb24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,9 @@ WORKDIR /app FROM base AS prod-deps RUN --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=package-lock.json,target=package-lock.json \ - --mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \ --mount=type=bind,source=packages/core/package.json,target=packages/core/package.json \ + --mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \ + --mount=type=bind,source=packages/insee/package.json,target=packages/insee/package.json \ --mount=type=cache,target=/root/.npm \ npm ci --omit=dev @@ -14,8 +15,9 @@ FROM base AS build ENV CYPRESS_INSTALL_BINARY=0 RUN --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=package-lock.json,target=package-lock.json \ - --mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \ --mount=type=bind,source=packages/core/package.json,target=packages/core/package.json \ + --mount=type=bind,source=packages/email/package.json,target=packages/email/package.json \ + --mount=type=bind,source=packages/insee/package.json,target=packages/insee/package.json \ --mount=type=cache,target=/root/.npm \ npm ci COPY tsconfig.json vite.config.mjs ./ diff --git a/package-lock.json b/package-lock.json index 1e45c348..491ac717 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@gouvfr-lasuite/crisp": "https://github.com/douglasduteil/crisp/releases/download/v1.6.1/douglasduteil-crisp-1.6.1.tgz", "@gouvfr-lasuite/proconnect.core": "workspace:*", "@gouvfr-lasuite/proconnect.email": "workspace:*", + "@gouvfr-lasuite/proconnect.insee": "workspace:*", "@gouvfr/dsfr": "^1.12.1", "@kitajs/html": "^4.2.4", "@kitajs/ts-html-plugin": "^4.1.0", @@ -1288,6 +1289,10 @@ "resolved": "packages/email", "link": true }, + "node_modules/@gouvfr-lasuite/proconnect.insee": { + "resolved": "packages/insee", + "link": true + }, "node_modules/@gouvfr/dsfr": { "version": "1.12.1", "resolved": "https://registry.npmjs.org/@gouvfr/dsfr/-/dsfr-1.12.1.tgz", @@ -7138,10 +7143,11 @@ } }, "node_modules/nock": { - "version": "13.5.4", - "resolved": "https://registry.npmjs.org/nock/-/nock-13.5.4.tgz", - "integrity": "sha512-yAyTfdeNJGGBFxWdzSKCBYxs5FxLbCg5X5Q4ets974hcQzG1+qCxvIyOo4j2Ry6MUlhWVMX4OoYDefAIIwupjw==", + "version": "13.5.6", + "resolved": "https://registry.npmjs.org/nock/-/nock-13.5.6.tgz", + "integrity": "sha512-o2zOYiCpzRqSzPj0Zt/dQ/DqZeYoaQ7TUonc/xUPjCGl9WeHpNbxgVvOquXYAaJzI0M9BXV3HTzG0p8IUAbBTQ==", "dev": true, + "license": "MIT", "dependencies": { "debug": "^4.1.0", "json-stringify-safe": "^5.0.1", @@ -10450,6 +10456,23 @@ "devDependencies": { "@tsconfig/node22": "^22.0.0" } + }, + "packages/insee": { + "name": "@gouvfr-lasuite/proconnect.insee", + "version": "0.2.0", + "license": "MIT", + "dependencies": { + "axios": "^1.7.7" + }, + "devDependencies": { + "@tsconfig/node22": "^22.0.0", + "@types/mocha": "^10.0.10", + "@types/node": "^22.10.2", + "chai": "^5.1.2", + "mocha": "^11.0.1", + "nock": "^13.5.6", + "tsx": "^4.19.2" + } } } } diff --git a/package.json b/package.json index 2e12560b..5bb3289c 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ "watch:js": "tsc --watch --preserveWatchOutput", "watch:node": "tsx --watch src/index.ts", "watch:workspaces:core": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.core", - "watch:workspaces:email": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.email" + "watch:workspaces:email": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.email", + "watch:workspaces:insee": "npm run dev --if-present --workspace=@gouvfr-lasuite/proconnect.insee" }, "prettier": { "plugins": [ @@ -51,6 +52,7 @@ "@gouvfr-lasuite/crisp": "https://github.com/douglasduteil/crisp/releases/download/v1.6.1/douglasduteil-crisp-1.6.1.tgz", "@gouvfr-lasuite/proconnect.core": "workspace:*", "@gouvfr-lasuite/proconnect.email": "workspace:*", + "@gouvfr-lasuite/proconnect.insee": "workspace:*", "@gouvfr/dsfr": "^1.12.1", "@kitajs/html": "^4.2.4", "@kitajs/ts-html-plugin": "^4.1.0", diff --git a/packages/insee/package.json b/packages/insee/package.json new file mode 100644 index 00000000..dc9dbf98 --- /dev/null +++ b/packages/insee/package.json @@ -0,0 +1,63 @@ +{ + "name": "@gouvfr-lasuite/proconnect.insee", + "version": "0.2.0", + "homepage": "https://github.com/numerique-gouv/moncomptepro/tree/master/packages/insee#readme", + "bugs": "https://github.com/numerique-gouv/moncomptepro/issues", + "repository": { + "type": "git", + "url": "git+https://github.com/numerique-gouv/moncomptepro.git", + "directory": "packages/insee" + }, + "license": "MIT", + "sideEffects": false, + "type": "module", + "imports": { + "#src/*": { + "types": "./src/*", + "default": "./dist/*" + } + }, + "exports": { + "./*": { + "require": { + "types": "./dist/*/index.d.ts", + "default": "./dist/*/index.js" + }, + "import": { + "types": "./dist/*/index.d.ts", + "default": "./dist/*/index.js" + }, + "types": "./dist/*/index.d.ts", + "default": "./dist/*/index.js" + } + }, + "scripts": { + "build": "tsc --project tsconfig.lib.json", + "check": "npm run build -- --noEmit", + "dev": "npm run build -- --watch --preserveWatchOutput", + "test": "mocha" + }, + "mocha": { + "reporter": "spec", + "require": [ + "tsx" + ], + "spec": "src/**/*.test.ts" + }, + "dependencies": { + "axios": "^1.7.7" + }, + "devDependencies": { + "@tsconfig/node22": "^22.0.0", + "@types/mocha": "^10.0.10", + "@types/node": "^22.10.2", + "chai": "^5.1.2", + "mocha": "^11.0.1", + "nock": "^13.5.6", + "tsx": "^4.19.2" + }, + "publishConfig": { + "access": "public", + "provenance": true + } +} diff --git a/packages/insee/src/token/get-insee-access-token.test.ts b/packages/insee/src/token/get-insee-access-token.test.ts new file mode 100644 index 00000000..c1b42aa7 --- /dev/null +++ b/packages/insee/src/token/get-insee-access-token.test.ts @@ -0,0 +1,26 @@ +// + +import { expect } from "chai"; +import { describe, it } from "mocha"; +import nock from "nock"; +import { getInseeAccessTokenFactory } from "./get-insee-access-token.js"; + +// + +const getInseeAccessToken = getInseeAccessTokenFactory({ + consumerKey: "🔑", + consumerSecret: "㊙️", +}); + +describe("GetInseeAccessToken", () => { + it("should return 🛂 access token", async () => { + nock("https://api.insee.fr").post("/token").reply(200, { + access_token: "🛂", + scope: "am_application_scope default", + token_type: "Bearer", + expires_in: 123456, + }); + const access_token = await getInseeAccessToken(); + expect(access_token).to.be.equal("🛂"); + }); +}); diff --git a/packages/insee/src/token/get-insee-access-token.ts b/packages/insee/src/token/get-insee-access-token.ts new file mode 100644 index 00000000..6f4ec0ff --- /dev/null +++ b/packages/insee/src/token/get-insee-access-token.ts @@ -0,0 +1,42 @@ +// + +import axios, { type AxiosRequestConfig, type AxiosResponse } from "axios"; + +// + +export type InseeCredentials = { + consumerKey: string; + consumerSecret: string; +}; +export type GetTokenReponse = { + access_token: string; + scope: "am_application_scope default"; + token_type: "Bearer"; + expires_in: number; +}; + +// + +export function getInseeAccessTokenFactory( + credentials: InseeCredentials, + config?: AxiosRequestConfig, +) { + return async function getInseeAccessToken() { + const { + data: { access_token }, + }: AxiosResponse = await axios.post( + "https://api.insee.fr/token", + "grant_type=client_credentials", + { + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + auth: { + username: credentials.consumerKey, + password: credentials.consumerSecret, + }, + ...config, + }, + ); + + return access_token; + }; +} diff --git a/packages/insee/src/token/index.ts b/packages/insee/src/token/index.ts new file mode 100644 index 00000000..ca94dd9a --- /dev/null +++ b/packages/insee/src/token/index.ts @@ -0,0 +1,3 @@ +// + +export * from "./get-insee-access-token.js"; diff --git a/packages/insee/tsconfig.json b/packages/insee/tsconfig.json new file mode 100644 index 00000000..ec2a6345 --- /dev/null +++ b/packages/insee/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "composite": true, + "declaration": true, + "declarationMap": true, + "outDir": "./dist", + "rootDir": "src", + "types": ["node"], + "module": "NodeNext", + "moduleResolution": "nodenext", + "verbatimModuleSyntax": true, + "paths": { + "#src/*": ["./src/*"] + } + }, + "extends": "@tsconfig/node22/tsconfig.json", + "references": [] +} diff --git a/packages/insee/tsconfig.lib.json b/packages/insee/tsconfig.lib.json new file mode 100644 index 00000000..3ba35441 --- /dev/null +++ b/packages/insee/tsconfig.lib.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./src" + }, + "exclude": ["src/**/*.test.ts"], + "extends": "./tsconfig.json", + "include": ["src"] +}