forked from copilot-platforms/custom-app-base
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor to fetch token from url * Get api token while saving setting * Change ts targe to es2017 * Make settings elements falsy check * Add copilot variables in example * Update target * Test * Try with es5 * what about es6 * Add peer dependency for next * Just yarn lock change * Remove peer dependencies * Remove local dev env variable
- Loading branch information
Showing
14 changed files
with
539 additions
and
261 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
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 was deleted.
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
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,27 @@ | ||
import { z } from 'zod'; | ||
|
||
export const MeResponseSchema = z.object({ | ||
id: z.string(), | ||
givenName: z.string(), | ||
familyName: z.string(), | ||
email: z.string(), | ||
portalName: z.string(), | ||
}); | ||
export type MeResponse = z.infer<typeof MeResponseSchema>; | ||
|
||
export const ClientResponseSchema = z.object({ | ||
id: z.string(), | ||
givenName: z.string(), | ||
familyName: z.string(), | ||
email: z.string(), | ||
companyId: z.string(), | ||
customFields: z.record(z.string(), z.union([z.string(), z.array(z.string())])), | ||
}); | ||
export type ClientResponse = z.infer<typeof ClientResponseSchema>; | ||
|
||
export const CompanyResponseSchema = z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
iconImageUrl: z.string(), | ||
}); | ||
export type CompanyResponse = z.infer<typeof CompanyResponseSchema>; |
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
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 |
---|---|---|
@@ -1,73 +1,40 @@ | ||
import { Message, SendMessageErrorResponse, SendMessageRequest } from '@/types/message'; | ||
|
||
const BaseApiURL = 'https://api-beta.copilot.com/v1'; | ||
|
||
export type MeResponse = { | ||
id: string; | ||
givenName: string; | ||
familyName: string; | ||
email: string; | ||
portalName: string; | ||
}; | ||
|
||
type ClientCustomField = string | string[]; | ||
|
||
export type Client = { | ||
id: string; | ||
givenName: string; | ||
familyName: string; | ||
email: string; | ||
companyId: string; | ||
customFields: Record<string, ClientCustomField>; | ||
}; | ||
|
||
export type Company = { | ||
id: string; | ||
name: string; | ||
iconImageUrl: string; | ||
}; | ||
import { copilotApi } from 'copilot-node-sdk'; | ||
|
||
enum Method { | ||
GET = 'GET', | ||
POST = 'POST', | ||
PUT = 'PUT', | ||
PATCH = 'PATCH', | ||
DELETE = 'DELETE', | ||
} | ||
import { Message, SendMessageErrorResponse, SendMessageRequest } from '@/types/message'; | ||
import appConfig from '@/config/app'; | ||
import { DefaultService as Copilot } from 'copilot-node-sdk/codegen/api/services/DefaultService'; | ||
import { | ||
ClientResponse, | ||
ClientResponseSchema, | ||
CompanyResponse, | ||
CompanyResponseSchema, | ||
MeResponse, | ||
MeResponseSchema, | ||
} from '@/types/common'; | ||
|
||
export class CopilotAPI { | ||
apiKey: string; | ||
copilot: typeof Copilot; | ||
|
||
constructor(apiKey: string) { | ||
this.apiKey = apiKey; | ||
} | ||
|
||
async sendApiData<T>(path: string, method: Method = Method.GET, payload?: unknown): Promise<T> { | ||
const response = await fetch(`${BaseApiURL}/${path}`, { | ||
headers: { | ||
'x-api-key': this.apiKey, | ||
}, | ||
method: method, | ||
body: payload ? JSON.stringify(payload) : undefined, | ||
constructor(apiToken: string) { | ||
this.copilot = copilotApi({ | ||
apiKey: appConfig.copilotApiKey, | ||
token: apiToken, | ||
}); | ||
|
||
const data = await response.json(); | ||
return data; | ||
} | ||
|
||
async me() { | ||
return this.sendApiData<MeResponse>('me'); | ||
async me(): Promise<MeResponse> { | ||
return MeResponseSchema.parse(await this.copilot.getUserAndPortalInfo()); | ||
} | ||
|
||
async getClient(clientId: string) { | ||
return this.sendApiData<Client>(`clients/${clientId}`); | ||
async getClient(clientId: string): Promise<ClientResponse> { | ||
return ClientResponseSchema.parse(await this.copilot.retrieveAClient({ id: clientId })); | ||
} | ||
|
||
async getCompany(companyId: string) { | ||
return this.sendApiData<Company>(`companies/${companyId}`); | ||
async getCompany(companyId: string): Promise<CompanyResponse> { | ||
return CompanyResponseSchema.parse(await this.copilot.retrieveACompany({ id: companyId })); | ||
} | ||
|
||
async sendMessage(payload: SendMessageRequest): Promise<Message | SendMessageErrorResponse> { | ||
return this.sendApiData<Message | SendMessageErrorResponse>(`messages`, Method.POST, payload); | ||
async sendMessage(payload: SendMessageRequest): Promise<Partial<Message> | SendMessageErrorResponse> { | ||
return this.copilot.sendAMessage({ requestBody: payload }); | ||
} | ||
} |
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
Oops, something went wrong.