From ddcabd7a0d9d56e5348f7adb46138ffe40766514 Mon Sep 17 00:00:00 2001 From: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com> Date: Tue, 14 Jan 2025 15:00:26 +0100 Subject: [PATCH] fix(types): copy SDK types to @types (#3302) ## Changes Contributes to https://linear.app/nango/issue/NAN-2265/remove-shared-from-cli - Copy SDK types to `@nangohq/types` It's to prepare the field when I introduce a dedicated package for the SDK, so we can reuse types across the codebase. Those types are not used at the moment. (see #3297 for full PR) - Fix types for pagination It wasn't using a discriminated object which makes type narrowing impossible - Fix types for response saver --- .../lib/services/response-saver.service.ts | 7 +-- packages/node-client/lib/index.ts | 2 +- packages/node-client/lib/types.ts | 9 +-- packages/shared/lib/sdk/sync.unit.test.ts | 7 +-- packages/types/lib/auth/api.ts | 5 ++ .../types/lib/environment/variable/index.ts | 4 ++ packages/types/lib/index.ts | 2 + packages/types/lib/proxy/api.ts | 14 +++-- packages/types/lib/runner/sdk.ts | 59 +++++++++++++++++++ 9 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 packages/types/lib/environment/variable/index.ts create mode 100644 packages/types/lib/runner/sdk.ts diff --git a/packages/cli/lib/services/response-saver.service.ts b/packages/cli/lib/services/response-saver.service.ts index f95f67ffa0b..1e5e2c4b1ef 100644 --- a/packages/cli/lib/services/response-saver.service.ts +++ b/packages/cli/lib/services/response-saver.service.ts @@ -2,8 +2,7 @@ import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; -import type { Connection } from '@nangohq/shared'; -import type { Metadata } from '@nangohq/types'; +import type { GetPublicConnection, Metadata } from '@nangohq/types'; const FILTER_HEADERS = [ 'authorization', @@ -77,10 +76,10 @@ export function onAxiosRequestFulfilled({ const directoryName = `${process.env['NANGO_MOCKS_RESPONSE_DIRECTORY'] ?? ''}${providerConfigKey}`; if (response.request.path.includes(`/connection/${connectionId}?provider_config_key=${providerConfigKey}`)) { - const connection = response.data as Connection; + const connection = response.data as GetPublicConnection['Success']; // getConnection could be getMetadata as well which would be cached - saveResponse>({ + saveResponse>({ directoryName, data: { metadata: connection.metadata as Metadata, connection_config: connection.connection_config }, customFilePath: 'mocks/nango/getConnection.json' diff --git a/packages/node-client/lib/index.ts b/packages/node-client/lib/index.ts index b03c47496ea..dc7e9bdadef 100644 --- a/packages/node-client/lib/index.ts +++ b/packages/node-client/lib/index.ts @@ -6,6 +6,7 @@ import https from 'node:https'; import type { ApiKeyCredentials, AppCredentials, + OAuth1Token, AppStoreCredentials, BasicApiCredentials, CredentialsCommon, @@ -37,7 +38,6 @@ import type { Metadata, MetadataChangeResponse, NangoProps, - OAuth1Token, ProxyConfiguration, RecordMetadata, StandardNangoConfig, diff --git a/packages/node-client/lib/types.ts b/packages/node-client/lib/types.ts index 54a2f94a30b..8580bc9f819 100644 --- a/packages/node-client/lib/types.ts +++ b/packages/node-client/lib/types.ts @@ -14,6 +14,7 @@ import type { HTTP_METHOD, NangoSyncEndpointV2, AllAuthCredentials, + OAuth1Token, OAuth1Credentials, OAuth2Credentials, OAuth2ClientCredentials, @@ -58,6 +59,7 @@ export type { AuthModeType, AuthModes, AllAuthCredentials, + OAuth1Token, OAuth1Credentials, OAuth2Credentials, OAuth2ClientCredentials, @@ -105,11 +107,6 @@ export interface CreateConnectionOAuth1 extends OAuth1Credentials { type: AuthModes['OAuth1']; } -export interface OAuth1Token { - oAuthToken: string; - oAuthTokenSecret: string; -} - export interface CreateConnectionOAuth2 extends OAuth2Credentials { connection_id: string; provider_config_key: string; @@ -128,7 +125,7 @@ export interface ProxyConfiguration { data?: unknown; retries?: number; baseUrlOverride?: string; - decompress?: boolean; + decompress?: boolean | string; responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'; retryOn?: number[] | null; } diff --git a/packages/shared/lib/sdk/sync.unit.test.ts b/packages/shared/lib/sdk/sync.unit.test.ts index e5ffa9863f1..21398bef7be 100644 --- a/packages/shared/lib/sdk/sync.unit.test.ts +++ b/packages/shared/lib/sdk/sync.unit.test.ts @@ -3,10 +3,9 @@ import { Nango } from '@nangohq/node'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { mockErrorManagerReport } from '../utils/error.manager.mocks.js'; import type { Config, SyncConfig } from '../models/index.js'; -import type { Provider } from '@nangohq/types'; +import type { CursorPagination, LinkPagination, OffsetPagination, Pagination, Provider } from '@nangohq/types'; import configService from '../services/config.service.js'; import * as providerService from '../services/providers.js'; -import type { CursorPagination, LinkPagination, OffsetPagination } from '../models/Proxy.js'; import type { NangoProps } from './sync.js'; import { NangoAction, NangoSync } from './sync.js'; import { isValidHttpUrl } from '../utils/utils.js'; @@ -410,12 +409,12 @@ describe('Pagination', () => { ); }); - const stubProviderTemplate = (paginationConfig: CursorPagination | OffsetPagination | LinkPagination) => { + const stubProviderTemplate = (paginationConfig: Pagination) => { const provider: Provider = buildTemplate(paginationConfig); vi.spyOn(providerService, 'getProvider').mockImplementation(() => provider); }; - const buildTemplate = (paginationConfig: CursorPagination | OffsetPagination | LinkPagination): Provider => { + const buildTemplate = (paginationConfig: Pagination): Provider => { return { display_name: 'test', auth_mode: 'OAUTH2', diff --git a/packages/types/lib/auth/api.ts b/packages/types/lib/auth/api.ts index 7706aa9b4be..4e3e3c53dc7 100644 --- a/packages/types/lib/auth/api.ts +++ b/packages/types/lib/auth/api.ts @@ -57,6 +57,11 @@ export interface CredentialsCommon> { raw: T; } +export interface OAuth1Token { + oAuthToken: string; + oAuthTokenSecret: string; +} + export interface BasicApiCredentials { type: AuthModes['Basic']; username: string; diff --git a/packages/types/lib/environment/variable/index.ts b/packages/types/lib/environment/variable/index.ts new file mode 100644 index 00000000000..c0348c6f38a --- /dev/null +++ b/packages/types/lib/environment/variable/index.ts @@ -0,0 +1,4 @@ +export interface EnvironmentVariable { + name: string; + value: string; +} diff --git a/packages/types/lib/index.ts b/packages/types/lib/index.ts index 21d65fcae3d..1d324a985c7 100644 --- a/packages/types/lib/index.ts +++ b/packages/types/lib/index.ts @@ -45,6 +45,7 @@ export type * from './connect/api.js'; export type * from './connect/session.js'; export type * from './endUser/index.js'; export type * from './runner/index.js'; +export type * from './runner/sdk.js'; export type * from './nangoYaml/index.js'; @@ -52,6 +53,7 @@ export type * from './environment/db.js'; export type * from './environment/api/index.js'; export type * from './environment/api/webhook.js'; export type * from './environment/api/otlp.js'; +export type * from './environment/variable/index.js'; export type * from './webhooks/api.js'; export type * from './webhooks/http.api.js'; export type * from './flow/http.api.js'; diff --git a/packages/types/lib/proxy/api.ts b/packages/types/lib/proxy/api.ts index a2623a7e961..0b28277548a 100644 --- a/packages/types/lib/proxy/api.ts +++ b/packages/types/lib/proxy/api.ts @@ -55,26 +55,30 @@ export enum PaginationType { OFFSET = 'offset' } -export interface Pagination { - type: string; +export interface PaginationBase { limit?: number; response_path?: string; limit_name_in_request: string; } -export interface CursorPagination extends Pagination { +export type Pagination = CursorPagination | LinkPagination | OffsetPagination; + +export interface CursorPagination extends PaginationBase { + type: 'cursor'; cursor_path_in_response: string; cursor_name_in_request: string; } -export interface LinkPagination extends Pagination { +export interface LinkPagination extends PaginationBase { + type: 'link'; link_rel_in_response_header?: string; link_path_in_response_body?: string; } export type OffsetCalculationMethod = 'per-page' | 'by-response-size'; -export interface OffsetPagination extends Pagination { +export interface OffsetPagination extends PaginationBase { + type: 'offset'; offset_name_in_request: string; offset_start_value?: number; offset_calculation_method?: OffsetCalculationMethod; diff --git a/packages/types/lib/runner/sdk.ts b/packages/types/lib/runner/sdk.ts new file mode 100644 index 00000000000..1b234e63ed3 --- /dev/null +++ b/packages/types/lib/runner/sdk.ts @@ -0,0 +1,59 @@ +import type { AxiosError, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse } from 'axios'; +import type { RunnerFlags } from '.'; +import type { Metadata } from '../connection/db'; +import type { DBTeam } from '../team/db'; +import type { DBSyncConfig } from '../syncConfigs/db'; + +export interface RunArgs { + sync: string; + connectionId: string; + lastSyncDate?: string; + useServerLastSyncDate?: boolean; + input?: object; + metadata?: Metadata; + autoConfirm: boolean; + debug: boolean; + optionalEnvironment?: string; + optionalProviderConfigKey?: string; +} + +export interface DryRunServiceInterface { + run: (options: RunArgs, debug?: boolean) => Promise; +} + +export interface NangoProps { + scriptType: 'sync' | 'action' | 'webhook' | 'on-event'; + host?: string; + secretKey: string; + team?: Pick; + connectionId: string; + environmentId: number; + environmentName?: string; + activityLogId?: string | undefined; + providerConfigKey: string; + provider: string; + lastSyncDate?: Date; + syncId?: string | undefined; + nangoConnectionId?: number; + syncJobId?: number | undefined; + track_deletes?: boolean; + attributes?: object | undefined; + abortSignal?: AbortSignal; + syncConfig: DBSyncConfig; + runnerFlags: RunnerFlags; + debug: boolean; + startedAt: Date; + endUser: { id: number; endUserId: string | null; orgId: string | null } | null; + + axios?: { + request?: AxiosInterceptorManager; + response?: { + onFulfilled: (value: AxiosResponse) => AxiosResponse | Promise; + onRejected: (value: unknown) => AxiosError | Promise; + }; + }; +} + +export interface UserLogParameters { + level?: 'info' | 'debug' | 'error' | 'warn' | 'http' | 'verbose' | 'silly'; +}