Skip to content

Commit

Permalink
Microsoft OneDrive service root endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-blacklist committed Feb 23, 2025
1 parent a763241 commit 2f281fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import type { ICredentialType, INodeProperties } from 'n8n-workflow';

const enum EndpointNames {
Global = 'Microsoft Graph global service',
GovCloud = 'Microsoft Graph for US Government L4',
DoDCloud = 'Microsoft Graph for US Government L5 (DOD)',
China = 'Microsoft Graph China operated by 21Vianet',
}

const endpoints: Record<EndpointNames, string> = {
[EndpointNames.Global]: 'https://graph.microsoft.com',
[EndpointNames.GovCloud]: 'https://graph.microsoft.us',
[EndpointNames.DoDCloud]: 'https://dod-graph.microsoft.us',
[EndpointNames.China]: 'https://microsoftgraph.chinacloudapi.cn',
};

export class MicrosoftOneDriveOAuth2Api implements ICredentialType {
name = 'microsoftOneDriveOAuth2Api';

Expand All @@ -17,5 +31,16 @@ export class MicrosoftOneDriveOAuth2Api implements ICredentialType {
type: 'hidden',
default: 'openid offline_access Files.ReadWrite.All',
},
{
displayName: 'Endpoint',
description: 'The service root endpoint to use when connecting to the Outlook API.',
name: 'graphEndpoint',
type: 'options',
default: endpoints[EndpointNames.Global],
options: Object.keys(endpoints).map((name) => ({
name,
value: endpoints[name as EndpointNames],
})),
},
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export async function microsoftApiRequest(
headers: IDataObject = {},
option: IDataObject = { json: true },
): Promise<any> {
const credentials = await this.getCredentials('microsoftOneDriveOAuth2Api');
const options: IRequestOptions = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: uri || `https://graph.microsoft.com/v1.0/me${resource}`,
uri: uri || `${credentials.graphEndpoint}/v1.0/me${resource}`,
};
try {
Object.assign(options, option);
Expand Down Expand Up @@ -145,13 +146,15 @@ export async function getPath(
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
itemId: string,
): Promise<string> {
const credentials = await this.getCredentials('microsoftOneDriveOAuth2Api');

const responseData = (await microsoftApiRequest.call(
this,
'GET',
'',
{},
{},
`https://graph.microsoft.com/v1.0/me/drive/items/${itemId}`,
`${credentials.graphEndpoint}/v1.0/me/drive/items/${itemId}`,
)) as IDataObject;
if (responseData.folder) {
return (responseData?.parentReference as IDataObject)?.path + `/${responseData?.name}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ export class MicrosoftOneDriveTrigger implements INodeType {

async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const workflowData = this.getWorkflowStaticData('node');
const credentials = await this.getCredentials('microsoftOneDriveOAuth2Api');
const graphEndpoint = credentials.graphEndpoint as string;

let responseData: IDataObject[];

const lastLink: string =
(workflowData.LastLink as string) ||
'https://graph.microsoft.com/v1.0/me/drive/root/delta?token=latest';
(workflowData.LastLink as string) || `${graphEndpoint}/v1.0/me/drive/root/delta?token=latest`;

const now = DateTime.now().toUTC();
const start = DateTime.fromISO(workflowData.lastTimeChecked as string) || now;
Expand All @@ -72,7 +74,7 @@ export class MicrosoftOneDriveTrigger implements INodeType {
'',
{},
{},
'https://graph.microsoft.com/v1.0/me/drive/root/delta',
`${graphEndpoint}/v1.0/me/drive/root/delta`,
)
).value as IDataObject[];
} else {
Expand Down

0 comments on commit 2f281fd

Please sign in to comment.