-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(insee): extract insee connector to individual pkg (#912)
- Loading branch information
1 parent
c406e75
commit ebd23fb
Showing
49 changed files
with
816 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,11 +108,11 @@ jobs: | |
with: | ||
cache: "npm" | ||
node-version-file: package.json | ||
- run: npm ci --include=dev | ||
- run: npm ci | ||
- run: npm run build:workspaces | ||
- run: npm run migrate up | ||
- run: npm run fixtures:load-ci -- cypress/e2e/${{ matrix.e2e_test }}/fixtures.sql | ||
- run: npm run update-organization-info -- 500 | ||
- run: npm run build:workspaces | ||
- name: Cypress run | ||
uses: cypress-io/[email protected] | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "./dist/*/index.d.ts", | ||
"default": "./dist/*/index.js" | ||
} | ||
}, | ||
"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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
|
||
import { expect } from "chai"; | ||
import { describe, it } from "mocha"; | ||
import nock from "nock"; | ||
import { findBySirenFactory } from "./find-by-siren.js"; | ||
|
||
// | ||
|
||
const findBySiren = findBySirenFactory({ | ||
getInseeAccessToken: async () => "SECRET_INSEE_TOKEN", | ||
}); | ||
|
||
describe("findBySiren", () => { | ||
it("should return an establishment", async () => { | ||
nock("https://api.insee.fr") | ||
.get( | ||
"/entreprises/sirene/siret?q=siren:200071843 AND etablissementSiege:true", | ||
) | ||
.reply(200, { etablissements: [{ siren: "🦄" }] }); | ||
|
||
const establishment = await findBySiren("200071843"); | ||
expect(establishment).to.be.deep.equal({ siren: "🦄" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
|
||
import type { GetInseeAccessTokenHandler } from "#src/api"; | ||
import { InvalidSirenError } from "#src/errors"; | ||
import type { InseeEtablissement } from "#src/types"; | ||
import axios, { type AxiosRequestConfig, type AxiosResponse } from "axios"; | ||
|
||
// | ||
|
||
type FactoryDependencies = { | ||
getInseeAccessToken: GetInseeAccessTokenHandler; | ||
config?: AxiosRequestConfig; | ||
}; | ||
|
||
type EtablissementSearchResponse = { | ||
header: { | ||
total: number; | ||
debut: number; | ||
nombre: number; | ||
}; | ||
etablissements: InseeEtablissement[]; | ||
}; | ||
|
||
export function findBySirenFactory({ | ||
getInseeAccessToken, | ||
config, | ||
}: FactoryDependencies) { | ||
return async function findBySiren(siren: string) { | ||
const token = await getInseeAccessToken(); | ||
const { data }: AxiosResponse<EtablissementSearchResponse> = | ||
await axios.get( | ||
`https://api.insee.fr/entreprises/sirene/siret?q=siren:${siren} AND etablissementSiege:true`, | ||
{ | ||
headers: { Authorization: `Bearer ${token}` }, | ||
...config, | ||
}, | ||
); | ||
|
||
const establishment = data.etablissements.at(0); | ||
|
||
if (!establishment) { | ||
throw new InvalidSirenError(); | ||
} | ||
|
||
return establishment; | ||
}; | ||
} | ||
|
||
export type FindBySirenHandler = ReturnType<typeof findBySirenFactory>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
|
||
import { expect } from "chai"; | ||
import { describe, it } from "mocha"; | ||
import nock from "nock"; | ||
import { findBySiretFactory } from "./find-by-siret.js"; | ||
|
||
// | ||
|
||
const findBySiret = findBySiretFactory({ | ||
getInseeAccessToken: async () => "SECRET_INSEE_TOKEN", | ||
}); | ||
|
||
describe("findBySiret", () => { | ||
it("should return an establishment", async () => { | ||
nock("https://api.insee.fr") | ||
.get("/entreprises/sirene/siret/20007184300060") | ||
.reply(200, { etablissement: { siren: "🦄" } }); | ||
|
||
const establishment = await findBySiret("20007184300060"); | ||
expect(establishment).to.be.deep.equal({ siren: "🦄" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
|
||
import type { GetInseeAccessTokenHandler } from "#src/api"; | ||
import type { InseeEtablissement } from "#src/types"; | ||
import type { AxiosRequestConfig, AxiosResponse } from "axios"; | ||
import axios from "axios"; | ||
|
||
// | ||
|
||
type FactoryDependencies = { | ||
getInseeAccessToken: GetInseeAccessTokenHandler; | ||
config?: AxiosRequestConfig; | ||
}; | ||
type EtablissementSearchBySiretResponse = { | ||
etablissement: InseeEtablissement; | ||
}; | ||
|
||
export function findBySiretFactory({ | ||
getInseeAccessToken, | ||
config, | ||
}: FactoryDependencies) { | ||
return async function findBySiret(siret: string) { | ||
const token = await getInseeAccessToken(); | ||
const { data }: AxiosResponse<EtablissementSearchBySiretResponse> = | ||
await axios.get( | ||
`https://api.insee.fr/entreprises/sirene/siret/${siret}`, | ||
{ | ||
headers: { Authorization: `Bearer ${token}` }, | ||
...config, | ||
}, | ||
); | ||
|
||
return data.etablissement; | ||
}; | ||
} | ||
|
||
export type FindBySiretHandler = ReturnType<typeof findBySiretFactory>; |
Oops, something went wrong.