Skip to content

Commit

Permalink
fix(types): copy SDK types to @types (#3302)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
bodinsamuel authored Jan 14, 2025
1 parent 6cfbb80 commit ddcabd7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 20 deletions.
7 changes: 3 additions & 4 deletions packages/cli/lib/services/response-saver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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<Pick<Connection, 'metadata' | 'connection_config'>>({
saveResponse<Pick<GetPublicConnection['Success'], 'metadata' | 'connection_config'>>({
directoryName,
data: { metadata: connection.metadata as Metadata, connection_config: connection.connection_config },
customFilePath: 'mocks/nango/getConnection.json'
Expand Down
2 changes: 1 addition & 1 deletion packages/node-client/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import https from 'node:https';
import type {
ApiKeyCredentials,
AppCredentials,
OAuth1Token,
AppStoreCredentials,
BasicApiCredentials,
CredentialsCommon,
Expand Down Expand Up @@ -37,7 +38,6 @@ import type {
Metadata,
MetadataChangeResponse,
NangoProps,
OAuth1Token,
ProxyConfiguration,
RecordMetadata,
StandardNangoConfig,
Expand Down
9 changes: 3 additions & 6 deletions packages/node-client/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
HTTP_METHOD,
NangoSyncEndpointV2,
AllAuthCredentials,
OAuth1Token,
OAuth1Credentials,
OAuth2Credentials,
OAuth2ClientCredentials,
Expand Down Expand Up @@ -58,6 +59,7 @@ export type {
AuthModeType,
AuthModes,
AllAuthCredentials,
OAuth1Token,
OAuth1Credentials,
OAuth2Credentials,
OAuth2ClientCredentials,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/shared/lib/sdk/sync.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions packages/types/lib/auth/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export interface CredentialsCommon<T = Record<string, any>> {
raw: T;
}

export interface OAuth1Token {
oAuthToken: string;
oAuthTokenSecret: string;
}

export interface BasicApiCredentials {
type: AuthModes['Basic'];
username: string;
Expand Down
4 changes: 4 additions & 0 deletions packages/types/lib/environment/variable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface EnvironmentVariable {
name: string;
value: string;
}
2 changes: 2 additions & 0 deletions packages/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ 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';

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';
Expand Down
14 changes: 9 additions & 5 deletions packages/types/lib/proxy/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions packages/types/lib/runner/sdk.ts
Original file line number Diff line number Diff line change
@@ -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<string | void>;
}

export interface NangoProps {
scriptType: 'sync' | 'action' | 'webhook' | 'on-event';
host?: string;
secretKey: string;
team?: Pick<DBTeam, 'id' | 'name'>;
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<AxiosRequestConfig>;
response?: {
onFulfilled: (value: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
onRejected: (value: unknown) => AxiosError | Promise<AxiosError>;
};
};
}

export interface UserLogParameters {
level?: 'info' | 'debug' | 'error' | 'warn' | 'http' | 'verbose' | 'silly';
}

0 comments on commit ddcabd7

Please sign in to comment.