UNPKG

@microfox/linkedin-member-data-portability

Version:

A TypeScript SDK for LinkedIn Member Data Portability.

145 lines 4.65 kB
// src/linkedin-member-data-portabilitySdk.ts import { z } from "zod"; import { LinkedInOAuthSdk, LinkedInScope } from "@microfox/linkedin-oauth"; var ErrorSchema = z.object({ message: z.string(), serviceErrorCode: z.number(), status: z.number() }); var ChangelogEventSchema = z.object({ id: z.number(), capturedAt: z.number(), processedAt: z.number(), configVersion: z.number(), owner: z.string(), actor: z.string(), resourceName: z.string(), resourceId: z.string(), resourceUri: z.string(), method: z.enum(["CREATE", "UPDATE", "PARTIAL_UPDATE", "DELETE"]), methodName: z.string().optional(), activity: z.record(z.unknown()), processedActivity: z.record(z.unknown()), siblingActivities: z.array(z.record(z.unknown())), parentSiblingActivities: z.array(z.record(z.unknown())), activityId: z.string(), activityStatus: z.enum(["SUCCESS", "FAILURE", "SUCCESSFUL_REPLAY"]) }).describe("Changelog event schema"); var MemberSnapshotSchema = z.object({ snapshotDomain: z.string(), snapshotData: z.record(z.unknown()) }).describe("Member snapshot schema"); var MemberComplianceAuthorizationSchema = z.object({ regulatedAt: z.number(), memberComplianceAuthorizationKey: z.object({ developerApplication: z.string(), member: z.string() }), memberComplianceScopes: z.array(z.string()) }).describe("Member compliance authorization schema"); var LinkedInDataPortabilitySDK = class { constructor(config) { this.baseUrl = "https://api.linkedin.com/v2"; this.oauthSdk = new LinkedInOAuthSdk({ clientId: config.clientId, clientSecret: config.clientSecret, redirectUri: config.redirectUri, scopes: [LinkedInScope.OFFLINE_ACCESS, "r_dma_portability_3rd_party"] }); this.accessToken = config.accessToken; } async request(endpoint, method, params) { const url = new URL(`${this.baseUrl}${endpoint}`); if (params) { Object.entries(params).forEach(([key, value]) => { url.searchParams.append(key, value); }); } const response = await fetch(url.toString(), { method, headers: { Authorization: `Bearer ${this.accessToken}`, "LinkedIn-Version": "202312", "Content-Type": "application/json" } }); if (!response.ok) { const error = await response.json().catch(() => ({})); throw new Error( `LinkedIn API error: ${response.status} ${response.statusText}: ${error.message}` ); } return response.json(); } async validateAccessToken() { try { const result = await this.oauthSdk.validateAccessToken(this.accessToken); if (!result.isValid) { throw new Error("Invalid access token"); } } catch (error) { throw new Error( `Failed to validate access token: ${error instanceof Error ? error.message : "Unknown error"}` ); } } async refreshAccessToken(refreshToken) { try { const result = await this.oauthSdk.refreshAccessToken(refreshToken); this.accessToken = result.accessToken; return result; } catch (error) { throw new Error( `Failed to refresh access token: ${error instanceof Error ? error.message : "Unknown error"}` ); } } async getMemberChangeLogs(startTime) { await this.validateAccessToken(); const params = { q: "memberAndApplication" }; if (startTime) { params.startTime = startTime.toString(); } const response = await this.request( "/rest/memberChangeLogs", "GET", params ); return z.array(ChangelogEventSchema).parse(response); } async getMemberSnapshotData(domain) { await this.validateAccessToken(); const params = { q: "criteria" }; if (domain) { params.domain = domain; } const response = await this.request( "/rest/memberSnapshotData", "GET", params ); return z.array(MemberSnapshotSchema).parse(response); } async getMemberAuthorizations() { await this.validateAccessToken(); const response = await this.request("/rest/memberAuthorizations", "GET", { q: "memberAndApplication" }); return MemberComplianceAuthorizationSchema.parse(response); } async triggerMemberDataProcessing() { await this.validateAccessToken(); const response = await this.request( "/rest/memberAuthorizations", "POST" ); return z.object({ success: z.boolean() }).parse(response); } }; var createLinkedInSDK = (config) => { return new LinkedInDataPortabilitySDK(config); }; export { LinkedInDataPortabilitySDK, LinkedInScope, createLinkedInSDK }; //# sourceMappingURL=index.mjs.map