From 61876df898433e157a1801dc71b70df487faac3b Mon Sep 17 00:00:00 2001 From: James Pace Date: Mon, 29 Apr 2019 19:42:13 +0100 Subject: [PATCH] auth0: Expose more of the Job resource in management client (#34657) * Expose more of the Job resource in auth0 management client * Don't use disguised type assertion with generics, per the common mistakes in the README * Add additional job status * Document job.type discriminator for auto type assertion * Revert interface name change for sending verification emails --- types/auth0/auth0-tests.ts | 76 +++++++++++++++++++++++++++++++++++++- types/auth0/index.d.ts | 74 ++++++++++++++++++++++++++++++++----- 2 files changed, 139 insertions(+), 11 deletions(-) diff --git a/types/auth0/auth0-tests.ts b/types/auth0/auth0-tests.ts index cca6bf8373..ba3b5f832e 100644 --- a/types/auth0/auth0-tests.ts +++ b/types/auth0/auth0-tests.ts @@ -234,5 +234,79 @@ management.getClients({fields:['name','client_metadata'], include_fields:true}) // Handle the error }); -// Get all cients with params (with callback) +// Get all clients with params (with callback) management.getClients({fields:['name','client_metadata'], include_fields:true}, (err:Error, clients:auth0.Client[]) => {}); + +// Jobs +management.getJob({ + id: 'job_id' +}).then((job) => console.log((job).fields)); + +// job.type can be used as a discriminator for automatic type assertion (no casting needed) +management.getJob({ + id: 'job_id' +}).then((job) => { + if (job.type === 'users_export') { + console.log(job.fields); + } +}); + +management.getJob({ + id: 'job_id' +}, (err, data) => console.log((data).fields)); + +management.getJob({ + id: 'job_id' +}).then((job) => console.log((job).send_completion_email)); + +management.getJob({ + id: 'job_id' +}, (err, data) => console.log((data).send_completion_email)); + +management.getJob({ + id: 'job_id' +}).then((job) => console.log((job).id)); + +management.getJob({ + id: 'job_id' +}, (err, data) => console.log((data).id)); + +management.importUsers({ + users: "some file data", + connection_id: 'con_id', + upsert: true +}).then((results) => console.log(results)); + +management.importUsers({ + users: "some file data", + connection_id: 'con_id', + upsert: true +}, (err, data) => console.log(data)); + +management.exportUsers({ + connection_id: 'con_id', + fields: [ + { name: 'email', export_as: 'email_address' } + ], + format: "json", + limit: 500 +}).then((results) => console.log(results)); + +management.exportUsers({ + connection_id: 'con_id', + fields: [ + { name: 'email', export_as: 'email_address' } + ], + format: "json", + limit: 500 +}, (err, data) => console.log(data)); + +management.sendEmailVerification({ + client_id: 'client_id', + user_id: 'user_id' +}).then((results) => console.log(results)); + +management.sendEmailVerification({ + client_id: 'client_id', + user_id: 'user_id' +}, (err, data) => console.log(data)); diff --git a/types/auth0/index.d.ts b/types/auth0/index.d.ts index c2df7dee7c..6a867f9d4d 100644 --- a/types/auth0/index.d.ts +++ b/types/auth0/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/auth0/node-auth0 // Definitions by: Seth Westphal // Ian Howe -// Alex Bjørlig +// Alex Bjørlig // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 @@ -509,13 +509,64 @@ export interface StatsParams { to: string; } +export type Job = ImportUsersJob | ExportUsersJob | VerificationEmailJob; + +export type JobFormat = 'csv' | 'json'; + +export type JobStatus = 'pending' | 'processing' | 'completed' | 'failed'; + +export interface ExportUsersJob { + id: string; + type: 'users_export'; + status: JobStatus; + created_at?: string; + connection_id?: string; + fields?: ExportUserField[]; + location?: string; + format?: JobFormat; +} + +export interface ImportUsersJob { + id: string; + type: 'users_import'; + status: JobStatus; + created_at?: string; + connection_id?: string; + upsert?: boolean; + external_id?: string; + send_completion_email?: boolean; +} + +export interface VerificationEmailJob { + id: string; + type: 'verification_email'; + status: JobStatus; + created_at?: string; +} + export interface ImportUsersOptions { - connection_id: string; - users: string; + users: string; + connection_id: string; + upsert?: boolean; + external_id?: string; + send_completion_email?: boolean; +} + +export interface ExportUsersOptions { + connection_id?: string; + format?: JobFormat; + limit?: number; + fields?: ExportUserField[] } export interface UserIdParams { - user_id: string; + user_id: string; + client_id?: string; +} + +export interface ExportUserField { + name: string; + export_as?: string; } export interface PasswordChangeTicketParams { @@ -816,14 +867,17 @@ export class ManagementClient { // Jobs - getJob(params: ObjectWithId): Promise; - getJob(params: ObjectWithId, cb?: (err: Error, data: any) => void): void; + getJob(params: ObjectWithId): Promise; + getJob(params: ObjectWithId, cb?: (err: Error, data: Job) => void): void; - importUsers(data: ImportUsersOptions): Promise; - importUsers(data: ImportUsersOptions, cb?: (err: Error, data: any) => void): void; + importUsers(data: ImportUsersOptions): Promise; + importUsers(data: ImportUsersOptions, cb?: (err: Error, data: ImportUsersJob) => void): void; - sendEmailVerification(data: UserIdParams): Promise; - sendEmailVerification(data: UserIdParams, cb?: (err: Error, data: any) => void): void; + exportUsers(data: ExportUsersOptions): Promise; + exportUsers(data: ExportUsersOptions, cb?: (err: Error, data: ExportUsersJob) => void): void; + + sendEmailVerification(data: UserIdParams): Promise; + sendEmailVerification(data: UserIdParams, cb?: (err: Error, data: VerificationEmailJob) => void): void; // Tickets createPasswordChangeTicket(params: PasswordChangeTicketParams): Promise;