Skip to content

Commit

Permalink
refactor(insee): extract insee connector to individual pkg (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Dec 23, 2024
1 parent 67be832 commit e0d0d98
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/hip-houses-confess.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ 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

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 ./
Expand Down
29 changes: 26 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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",
Expand Down
63 changes: 63 additions & 0 deletions packages/insee/package.json
Original file line number Diff line number Diff line change
@@ -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
}
}
26 changes: 26 additions & 0 deletions packages/insee/src/token/get-insee-access-token.test.ts
Original file line number Diff line number Diff line change
@@ -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("🛂");
});
});
42 changes: 42 additions & 0 deletions packages/insee/src/token/get-insee-access-token.ts
Original file line number Diff line number Diff line change
@@ -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<GetTokenReponse> = 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;
};
}
3 changes: 3 additions & 0 deletions packages/insee/src/token/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//

export * from "./get-insee-access-token.js";
18 changes: 18 additions & 0 deletions packages/insee/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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": []
}
9 changes: 9 additions & 0 deletions packages/insee/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"exclude": ["src/**/*.test.ts"],
"extends": "./tsconfig.json",
"include": ["src"]
}

0 comments on commit e0d0d98

Please sign in to comment.