-
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
- Loading branch information
1 parent
67be832
commit e99da25
Showing
10 changed files
with
199 additions
and
6 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,5 @@ | ||
--- | ||
"@gouvfr-lasuite/proconnect.insee": minor | ||
--- | ||
|
||
♻️ Prélevement d'un partie du connecteur INSEE |
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": "./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 | ||
} | ||
} |
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,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("🛂"); | ||
}); | ||
}); |
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,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; | ||
}; | ||
} |
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,3 @@ | ||
// | ||
|
||
export * from "./get-insee-access-token.js"; |
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,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": [] | ||
} |
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"rootDir": "./src" | ||
}, | ||
"exclude": ["src/**/*.test.ts"], | ||
"extends": "./tsconfig.json", | ||
"include": ["src"] | ||
} |