-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement instance information getter
- Loading branch information
Showing
7 changed files
with
115 additions
and
3 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
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,22 @@ | ||
import { getPronoteInstanceInformation, defaultPawnoteFetcher } from "../src"; | ||
|
||
(async () => { | ||
const instance = await getPronoteInstanceInformation(defaultPawnoteFetcher, { | ||
pronoteURL: "https://demo.index-education.net/pronote" | ||
}); | ||
|
||
console.log("Root URL:", instance.pronoteRootURL); | ||
console.log("School Name:", instance.schoolName); | ||
console.log("Server Version:", instance.version); | ||
|
||
console.group("Available account types:", instance.accounts.length); | ||
instance.accounts.forEach(account => { | ||
console.log("->", account.name, `(${account.id})`); | ||
}) | ||
console.groupEnd(); | ||
|
||
if (instance.entURL) { | ||
console.log("ENT is activated, base URL is", instance.entURL); | ||
console.log("-> ENT token:", instance.entToken!); | ||
} | ||
})(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { makeApiHandler } from "~/utils/api"; | ||
import type { PronoteApiInstance, ApiInstance } from "./types"; | ||
import { cleanPronoteUrl } from "~/pronote/url"; | ||
import { PRONOTE_ACCOUNT_TYPES, PronoteApiAccountType } from "~/constants/accounts"; | ||
import { PRONOTE_INSTANCE_MOBILE_INFOS_PATH } from "~/constants/urls"; | ||
|
||
/** | ||
* Filter function to prevent TS issues. | ||
* Allows to check that every item is defined and make them typed to `PronoteApiAccountType`. | ||
*/ | ||
const isPronoteApiAccountType = (item: PronoteApiAccountType | undefined): item is PronoteApiAccountType => { | ||
return Boolean(item); | ||
}; | ||
|
||
/** | ||
* Takes an instance URL and return informations about it such as... | ||
* - available account types ; | ||
* - instance name ; | ||
* - base URL and potential ENT URL | ||
*/ | ||
export const callApiInstance = makeApiHandler<ApiInstance>(async (fetcher, input) => { | ||
const pronoteURL = cleanPronoteUrl(input.pronoteURL); | ||
const informationURL = `${pronoteURL}/${PRONOTE_INSTANCE_MOBILE_INFOS_PATH}`; | ||
|
||
const response = await fetcher(informationURL, { | ||
method: "GET" | ||
}); | ||
|
||
// Build the local date to get the timezone offset, right after. | ||
// const local_date = new Date(new Date().toLocaleString("fr-FR", { timeZone: req.body.timezone })); | ||
|
||
const data = await response.json<PronoteApiInstance["response"]>(); | ||
|
||
// Calculate the timezone offset between the server and the client. | ||
// const timezone_offset = local_date.getTime() - new Date(data.date).getTime(); | ||
|
||
// Filter the accounts to only keep the ones that are supported. | ||
const accounts = data.espaces.map((account) => PRONOTE_ACCOUNT_TYPES.find( | ||
(account_type) => account_type.path === account.URL | ||
)).filter(isPronoteApiAccountType); | ||
|
||
return { | ||
accounts, | ||
pronoteRootURL: pronoteURL, | ||
version: data.version[0], | ||
schoolName: data.nomEtab, | ||
entURL: data.CAS.actif ? data.CAS.casURL : undefined, | ||
entToken: data.CAS.actif ? data.CAS.jetonCAS : undefined | ||
}; | ||
}); |
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,36 @@ | ||
import { PronoteApiAccountId } from "~/constants/accounts"; | ||
|
||
export interface PronoteApiInstance { | ||
request: Record<string, never> | ||
|
||
response: { | ||
version: number[] | ||
date: string | ||
CAS: { actif: false } | { actif: true, casURL: string, jetonCAS: string } | ||
espaces: Array<{ nom: string, URL: string }> | ||
nomEtab: string | ||
} | ||
} | ||
|
||
export interface ApiInstance { | ||
input: { | ||
pronoteURL: string | ||
} | ||
|
||
output: { | ||
version: number; | ||
schoolName: string; | ||
|
||
accounts: Array<{ | ||
name: string | ||
id: PronoteApiAccountId | ||
}> | ||
|
||
pronoteRootURL: string | ||
|
||
/** URL of the ENT we have to handle. */ | ||
entURL?: string | ||
/** Used to generate new temporary passwords for Pronote after ENT login. */ | ||
entToken?: string | ||
} | ||
} |
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