Repository: xataio/client-ts
Pull request #165 (2022-05-11 10:53:40)

Make apiKey and branch optional
PR Review suggestion


Patch:
diff --git a/client/src/api/client.ts b/client/src/api/client.ts
index f573607b..8f74d61f 100644
--- a/client/src/api/client.ts
+++ b/client/src/api/client.ts
@@ -1,3 +1,4 @@
+import { getAPIKey } from '../schema/config';
 import { getFetchImplementation } from '../util/fetch';
 import type * as Types from './components';
 import { operationsByTag } from './components';
@@ -8,7 +9,7 @@ import type * as Schemas from './schemas';
 
 export interface XataApiClientOptions {
   fetch?: FetchImpl;
-  apiKey: string;
+  apiKey?: string;
   host?: HostProvider;
 }
 
@@ -17,12 +18,17 @@ export class XataApiClient {
 
   constructor(options: XataApiClientOptions) {
     const provider = options.host ?? 'production';
+    const apiKey = options?.apiKey ?? getAPIKey();
+
+    if (!apiKey) {
+      throw new Error('Could not resolve a valid apiKey');
+    }
 
     this.#extraProps = {
       apiUrl: getHostUrl(provider, 'main'),
       workspacesApiUrl: getHostUrl(provider, 'workspaces'),
       fetchImpl: getFetchImplementation(options.fetch),
-      apiKey: options.apiKey
+      apiKey
     };
   }
 
diff --git a/client/src/schema/repository.ts b/client/src/schema/repository.ts
index cab989de..b5aa438d 100644
--- a/client/src/schema/repository.ts
+++ b/client/src/schema/repository.ts
@@ -152,9 +152,15 @@ export class RestRepository<Data extends BaseData, Record extends XataRecord = D
   async #getFetchProps(): Promise<FetcherExtraProps> {
     const branch = await this.#client.getBranch();
 
+    const apiKey = this.#client.options.apiKey ?? getAPIKey();
+
+    if (!apiKey) {
+      throw new Error('Could not resolve a valid apiKey');
+    }
+
     return {
       fetchImpl: this.#fetch,
-      apiKey: this.#client.options.apiKey,
+      apiKey,
       apiUrl: '',
       // Instead of using workspace and dbBranch, we inject a probably CNAME'd URL
       workspacesApiUrl: (path, params) => {
@@ -462,11 +468,11 @@ export type XataClientOptions = {
    */
   fetch?: FetchImpl;
   databaseURL?: string;
-  branch: BranchStrategyOption;
+  branch?: BranchStrategyOption;
   /**
    * API key to be used. You can create one in your account settings at https://app.xata.io/settings.
    */
-  apiKey: string;
+  apiKey?: string;
   repositoryFactory?: RepositoryFactory;
 };
 
@@ -501,7 +507,7 @@ export class BaseClient<D extends Record<string, Repository<any>> = Record<strin
   options: XataClientOptions;
   public db!: D;
 
-  constructor(options: Partial<XataClientOptions> | undefined, links: Links = {}) {
+  constructor(options: XataClientOptions = {}, links: Links = {}) {
     this.options = resolveXataClientOptions(options);
     // Make this property not enumerable so it doesn't show up in console.dir, etc.
     Object.defineProperty(this.options, 'apiKey', { enumerable: false });
diff --git a/codegen/example/types.d.ts b/codegen/example/types.d.ts
index 3786fa48..d14b5e5b 100644
--- a/codegen/example/types.d.ts
+++ b/codegen/example/types.d.ts
@@ -1,23 +1,23 @@
 import { BaseClient, Repository, XataClientOptions, XataRecord } from '../../client/src';
 export interface Team {
-  name?: string | null;
-  labels?: string[] | null;
-  owner?: UserRecord | null;
+    name?: string | null;
+    labels?: string[] | null;
+    owner?: UserRecord | null;
 }
 export declare type TeamRecord = Team & XataRecord;
 export interface User {
-  email?: string | null;
-  full_name?: string | null;
-  address?: {
-    street?: string | null;
-    zipcode?: number | null;
-  } | null;
-  team?: TeamRecord | null;
+    email?: string | null;
+    full_name?: string | null;
+    address?: {
+        street?: string | null;
+        zipcode?: number | null;
+    } | null;
+    team?: TeamRecord | null;
 }
 export declare type UserRecord = User & XataRecord;
 export declare class XataClient extends BaseClient<{
-  teams: Repository<Team>;
-  users: Repository<User>;
+    "teams": Repository<Team>;
+    "users": Repository<User>;
 }> {
-  constructor(options?: XataClientOptions);
+    constructor(options?: XataClientOptions);
 }
diff --git a/codegen/example/xata.ts b/codegen/example/xata.ts
index 52f3239f..634b454b 100644
--- a/codegen/example/xata.ts
+++ b/codegen/example/xata.ts
@@ -23,7 +23,7 @@ export class XataClient extends BaseClient<{
   teams: Repository<Team>;
   users: Repository<User>;
 }> {
-  constructor(options?: Partial<XataClientOptions>) {
+  constructor(options?: XataClientOptions) {
     super({ databaseURL: 'https://test-r5vcv5.xata.sh/db/test', ...options }, links);
 
     const factory = options?.repositoryFactory || new RestRespositoryFactory();
diff --git a/codegen/src/codegen.ts b/codegen/src/codegen.ts
index c5a1018b..5c31c3b5 100644
--- a/codegen/src/codegen.ts
+++ b/codegen/src/codegen.ts
@@ -123,7 +123,7 @@ export async function generate({
     export class XataClient extends BaseClient<{
       ${tables.map((table) => `"${table.name}": Repository<${getTypeName(table.name)}>;`).join('\n')}
     }> {
-      constructor(options?: Partial<XataClientOptions>) {
+      constructor(options?: XataClientOptions) {
         super({ databaseURL: "https://${config.workspaceID}.xata.sh/db/${config.dbName}", ...options}, links);
 
         const factory = options?.repositoryFactory || new RestRespositoryFactory();


Test patch:
diff --git a/client/src/schema/index.test.ts b/client/src/schema/index.test.ts
index 4ba42592..8d9f50bc 100644
--- a/client/src/schema/index.test.ts
+++ b/client/src/schema/index.test.ts
@@ -5,7 +5,7 @@ interface User extends XataRecord {
   name: string;
 }
 
-const buildClient = (options: Partial<XataClientOptions> = {}) => {
+const buildClient = (options: XataClientOptions = {}) => {
   const {
     apiKey = '1234',
     databaseURL = 'https://my-workspace-5df34do.staging.xatabase.co/db/xata',
diff --git a/test/__snapshots__/codegen.test.ts.snap b/test/__snapshots__/codegen.test.ts.snap
index 94744ac8..f4646e05 100644
--- a/test/__snapshots__/codegen.test.ts.snap
+++ b/test/__snapshots__/codegen.test.ts.snap
@@ -82,7 +82,7 @@ export class XataClient extends BaseClient<{
   posts: Repository<Post>;
   people: Repository<Person>;
 }> {
-  constructor(options?: Partial<XataClientOptions>) {
+  constructor(options?: XataClientOptions) {
     super(
       {
         databaseURL: \\"https://my-workspace-gfdd12.xata.sh/db/hello-xata\\",