diff --git a/.gitignore b/.gitignore index 7a1537b..3b27ef8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea node_modules +dist/ diff --git a/README.md b/README.md index d19bca9..8e57e5d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # Mangrove-reviews-typescript -A vite-compatible typescript port of Mangrove.reviews + +This is a port of [mangrove-reviews](https://www.npmjs.com/package/mangrove-reviews), but which uses [jose](https://www.npmjs.com/package/jose) instead of [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken). +The latter has a dependency which uses `Stream`, which `vite` cannot handle. + +Another benefit is that this one has better typing* and should thus be more ergonomic to use. + +(*): typing is incomplete and only includes the very basic usecase of downloading and uploading reviews - the bare minimum of what I needed for MapComplete. +However, I'll gladly merge pull requests which add more types. + diff --git a/lib/Mangrove-reviews.ts b/lib/Mangrove-reviews.ts index f9ae2b0..fb5fafb 100644 --- a/lib/Mangrove-reviews.ts +++ b/lib/Mangrove-reviews.ts @@ -8,56 +8,56 @@ export interface QueryParameters { /** * Search for reviews that have this string in `sub` or `opinion` field. */ - q: string, + q?: string, /** * Search for review with this `signature` value. */ - signature: string + signature?: string /** * Reviews by issuer with the following PEM public key. */ - kid: string + kid?: string /** * Reviews issued at this UNIX time. */ - iat: number + iat?: number /** * Reviews with UNIX timestamp greater than this. */ - gt_iat: number + gt_iat?: number /** * Reviews of the given subject URI. */ - sub: string + sub?: string /** * Reviews with the given rating. */ - rating: number + rating?: number /** * Reviews with the given opinion. */ - opinion: string + opinion?: string /** * Maximum number of reviews to be returned. */ - limit: number + limit?: number /** * Get only reviews with opinion text. */ - opinionated: boolean + opinionated?: boolean /** * Include reviews of example subjects. */ - examples: boolean + examples?: boolean /** * Include aggregate information about review issuers. */ - issuers: boolean + issuers?: boolean /** * Include aggregate information about reviews of returned reviews. */ - maresi_subjects: boolean + maresi_subjects?: boolean } @@ -93,7 +93,7 @@ export class MangroveReviews { * @param {string} [api=ORIGINAL_API] API endpoint used to fetch the data. * @returns {Promise} Resolves to "true" in case of successful insertion or rejects with errors. */ - public static submitReview(jwt, api = MangroveReviews.ORIGINAL_API) { + public static submitReview(jwt: string, api: string = MangroveReviews.ORIGINAL_API) { return axios.put(`${api}/submit/${jwt}`) } @@ -103,7 +103,7 @@ export class MangroveReviews { * @param {Payload} payload Base {@link Payload} to be cleaned, it will be mutated. * @param {string} [api=ORIGINAL_API] - API endpoint used to fetch the data. */ - static async signAndSubmitReview(keypair, payload, api = MangroveReviews.ORIGINAL_API) { + static async signAndSubmitReview(keypair: CryptoKeyPair, payload: Review, api:string = MangroveReviews.ORIGINAL_API) { const jwt = await MangroveReviews.signReview(keypair, payload) return MangroveReviews.submitReview(jwt, api) } @@ -114,7 +114,20 @@ export class MangroveReviews { * @param api The api-endpoint to query; default: mangrove.reviews */ - public static async getReviews(query: QueryParameters, api = MangroveReviews.ORIGINAL_API): Promise { + public static async getReviews(query: QueryParameters, api = MangroveReviews.ORIGINAL_API): + Promise<{ + /** A list of reviews satisfying the query.*/ + reviews: { + signature: string, + jwt: string, + kid: string, + payload: Review, + scheme: "geo" | string + }[] , + /** A map from Review identifiers (urn:maresi:) to information about the reviews of that review. */ + maresi_subjects?: any[], + issuers?: any[] + }> { const {data} = await axios.get(`${api}/reviews`, { params: query, headers: {'Content-Type': 'application/json'} @@ -238,8 +251,7 @@ export class MangroveReviews { public static async privateToPem(key) { try { const exported: ArrayBuffer = await crypto.subtle.exportKey('pkcs8', key) - const exportedAsString = MangroveReviews.u8aToString(exported) - const exportedAsBase64 = window.btoa(exportedAsString) + const exportedAsBase64 = btoa(String.fromCharCode(...new Uint8Array(exported))); return `-----BEGIN PRIVATE KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----` } catch { // Workaround for Firefox webcrypto not working. @@ -255,8 +267,8 @@ export class MangroveReviews { */ public static async publicToPem(key: CryptoKey): Promise { const exported: ArrayBuffer = await crypto.subtle.exportKey('spki', key) - const exportedAsString = MangroveReviews.u8aToString(exported) - const exportedAsBase64 = btoa(exportedAsString) + const exportedAsBase64 = btoa(String.fromCharCode(...new Uint8Array(exported))); + // Do not add new lines so that its copyable from plain string representation. return `-----BEGIN PUBLIC KEY-----${exportedAsBase64}-----END PUBLIC KEY-----` } diff --git a/lib/Review.ts b/lib/Review.ts index f223dff..af497b0 100644 --- a/lib/Review.ts +++ b/lib/Review.ts @@ -5,7 +5,7 @@ export interface Metadata extends JWTPayload{ /** * Identity of the client used to leave the review, gets populated if not provided. */ - client_id: string + client_id?: string /** * Nickname of the reviewer. @@ -66,7 +66,7 @@ export interface Review { * Unix timestamp of when review was issued, * gets filled in automatically if not provided. */ - iat: number + iat?: number /** * Array of up to 5 images to be included. diff --git a/package.json b/package.json index 60d1306..48421a4 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "mangrove-reviews-typescript", - "version": "0.0.1", + "version": "0.0.2", "description": "A library to interface with Mangrove.reviews", "main": "lib/index.ts", "scripts": { - "build": "tsc -project .", + "build": "tsc -project ." }, "repository": { "type": "git", diff --git a/tsconfig.json b/tsconfig.json index d77253b..6acc14a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,9 +9,7 @@ "declaration": true, "outDir": "./dist", "rootDir": "lib", - "types": [ - "node" - ], + "types": [ ], "strict": true, "esModuleInterop": true, "resolveJsonModule": false,