From 14dde97b3517b93372487ee30bca17fa1936c4c0 Mon Sep 17 00:00:00 2001 From: Martin Sosic Date: Sat, 27 Jan 2024 13:21:58 +0100 Subject: [PATCH] [New SDK]: 'wasp/client/api'. --- .../src/auth/pages/OAuthCodeExchange.jsx | 2 +- .../data/Generator/templates/sdk/api/index.ts | 10 ++++--- .../templates/sdk/auth/email/actions/login.ts | 2 +- .../sdk/auth/email/actions/passwordReset.ts | 2 +- .../sdk/auth/email/actions/signup.ts | 2 +- .../sdk/auth/email/actions/verifyEmail.ts | 2 +- .../templates/sdk/auth/helpers/user.ts | 2 +- .../Generator/templates/sdk/auth/login.ts | 2 +- .../Generator/templates/sdk/auth/logout.ts | 2 +- .../Generator/templates/sdk/auth/signup.ts | 2 +- .../Generator/templates/sdk/auth/useAuth.ts | 4 +-- .../templates/sdk/operations/index.ts | 2 +- .../data/Generator/templates/sdk/package.json | 26 ++++++++++++++----- .../templates/sdk/server/api/index.ts | 5 ++-- .../sdk/webSocket/WebSocketProvider.tsx | 2 +- .../examples/todo-typescript/src/ChatPage.tsx | 2 +- 16 files changed, 43 insertions(+), 26 deletions(-) diff --git a/waspc/data/Generator/templates/react-app/src/auth/pages/OAuthCodeExchange.jsx b/waspc/data/Generator/templates/react-app/src/auth/pages/OAuthCodeExchange.jsx index 295cf5fa71..466fd90989 100644 --- a/waspc/data/Generator/templates/react-app/src/auth/pages/OAuthCodeExchange.jsx +++ b/waspc/data/Generator/templates/react-app/src/auth/pages/OAuthCodeExchange.jsx @@ -3,7 +3,7 @@ import React, { useEffect, useRef } from 'react' import { useHistory } from 'react-router-dom' import config from 'wasp/core/config' -import api from 'wasp/api' +import { api } from 'wasp/client/api' import { initSession } from 'wasp/auth/helpers/user' // After a user authenticates via an Oauth 2.0 provider, this is the page that diff --git a/waspc/data/Generator/templates/sdk/api/index.ts b/waspc/data/Generator/templates/sdk/api/index.ts index 971f010160..d066bd5448 100644 --- a/waspc/data/Generator/templates/sdk/api/index.ts +++ b/waspc/data/Generator/templates/sdk/api/index.ts @@ -4,7 +4,8 @@ import config from 'wasp/core/config' import { storage } from 'wasp/core/storage' import { apiEventsEmitter } from './events.js' -const api = axios.create({ +// PUBLIC API +export const api = axios.create({ baseURL: config.apiUrl, }) @@ -12,22 +13,26 @@ const WASP_APP_AUTH_SESSION_ID_NAME = 'sessionId' let waspAppAuthSessionId = storage.get(WASP_APP_AUTH_SESSION_ID_NAME) as string | undefined +// PRIVATE API (sdk) export function setSessionId(sessionId: string): void { waspAppAuthSessionId = sessionId storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId) apiEventsEmitter.emit('sessionId.set') } +// PRIVATE API (sdk) export function getSessionId(): string | undefined { return waspAppAuthSessionId } +// PRIVATE API (sdk) export function clearSessionId(): void { waspAppAuthSessionId = undefined storage.remove(WASP_APP_AUTH_SESSION_ID_NAME) apiEventsEmitter.emit('sessionId.clear') } +// PRIVATE API (sdk) export function removeLocalUserData(): void { waspAppAuthSessionId = undefined storage.clear() @@ -66,6 +71,7 @@ window.addEventListener('storage', (event) => { } }) +// PRIVATE API (sdk) /** * Takes an error returned by the app's API (as returned by axios), and transforms into a more * standard format to be further used by the client. It is also assumed that given API @@ -100,5 +106,3 @@ class WaspHttpError extends Error { this.data = data } } - -export default api diff --git a/waspc/data/Generator/templates/sdk/auth/email/actions/login.ts b/waspc/data/Generator/templates/sdk/auth/email/actions/login.ts index 72194361aa..2f2cfb4777 100644 --- a/waspc/data/Generator/templates/sdk/auth/email/actions/login.ts +++ b/waspc/data/Generator/templates/sdk/auth/email/actions/login.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import api, { handleApiError } from 'wasp/api'; +import { api, handleApiError } from 'wasp/client/api'; import { initSession } from '../../helpers/user'; export async function login(data: { email: string; password: string }): Promise { diff --git a/waspc/data/Generator/templates/sdk/auth/email/actions/passwordReset.ts b/waspc/data/Generator/templates/sdk/auth/email/actions/passwordReset.ts index 34c773ce23..ac721f2102 100644 --- a/waspc/data/Generator/templates/sdk/auth/email/actions/passwordReset.ts +++ b/waspc/data/Generator/templates/sdk/auth/email/actions/passwordReset.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import api, { handleApiError } from 'wasp/api'; +import { api, handleApiError } from 'wasp/client/api'; export async function requestPasswordReset(data: { email: string; }): Promise<{ success: boolean }> { try { diff --git a/waspc/data/Generator/templates/sdk/auth/email/actions/signup.ts b/waspc/data/Generator/templates/sdk/auth/email/actions/signup.ts index 60c0da2cff..cf882b2f71 100644 --- a/waspc/data/Generator/templates/sdk/auth/email/actions/signup.ts +++ b/waspc/data/Generator/templates/sdk/auth/email/actions/signup.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import api, { handleApiError } from 'wasp/api'; +import { api, handleApiError } from 'wasp/client/api'; export async function signup(data: { email: string; password: string }): Promise<{ success: boolean }> { try { diff --git a/waspc/data/Generator/templates/sdk/auth/email/actions/verifyEmail.ts b/waspc/data/Generator/templates/sdk/auth/email/actions/verifyEmail.ts index 10fa4308c0..fa96569bb3 100644 --- a/waspc/data/Generator/templates/sdk/auth/email/actions/verifyEmail.ts +++ b/waspc/data/Generator/templates/sdk/auth/email/actions/verifyEmail.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import api, { handleApiError } from 'wasp/api' +import { api, handleApiError } from 'wasp/client/api' export async function verifyEmail(data: { token: string diff --git a/waspc/data/Generator/templates/sdk/auth/helpers/user.ts b/waspc/data/Generator/templates/sdk/auth/helpers/user.ts index 498f2588a8..259a4c34b5 100644 --- a/waspc/data/Generator/templates/sdk/auth/helpers/user.ts +++ b/waspc/data/Generator/templates/sdk/auth/helpers/user.ts @@ -1,4 +1,4 @@ -import { setSessionId } from 'wasp/api' +import { setSessionId } from 'wasp/client/api' import { invalidateAndRemoveQueries } from 'wasp/operations/resources' export async function initSession(sessionId: string): Promise { diff --git a/waspc/data/Generator/templates/sdk/auth/login.ts b/waspc/data/Generator/templates/sdk/auth/login.ts index b18a09ec16..5d084ad794 100644 --- a/waspc/data/Generator/templates/sdk/auth/login.ts +++ b/waspc/data/Generator/templates/sdk/auth/login.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import api, { handleApiError } from 'wasp/api' +import { api, handleApiError } from 'wasp/client/api' import { initSession } from './helpers/user' export default async function login(username: string, password: string): Promise { diff --git a/waspc/data/Generator/templates/sdk/auth/logout.ts b/waspc/data/Generator/templates/sdk/auth/logout.ts index cc41b6989c..7f40b0cbf6 100644 --- a/waspc/data/Generator/templates/sdk/auth/logout.ts +++ b/waspc/data/Generator/templates/sdk/auth/logout.ts @@ -1,4 +1,4 @@ -import api, { removeLocalUserData } from 'wasp/api' +import { api, removeLocalUserData } from 'wasp/client/api' import { invalidateAndRemoveQueries } from 'wasp/operations/resources' export default async function logout(): Promise { diff --git a/waspc/data/Generator/templates/sdk/auth/signup.ts b/waspc/data/Generator/templates/sdk/auth/signup.ts index 4fe93239cf..b0c0930f78 100644 --- a/waspc/data/Generator/templates/sdk/auth/signup.ts +++ b/waspc/data/Generator/templates/sdk/auth/signup.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import api, { handleApiError } from 'wasp/api' +import { api, handleApiError } from 'wasp/client/api' export default async function signup(userFields: { username: string; password: string }): Promise { try { diff --git a/waspc/data/Generator/templates/sdk/auth/useAuth.ts b/waspc/data/Generator/templates/sdk/auth/useAuth.ts index b2b7a5b06e..bf2d7e63e2 100644 --- a/waspc/data/Generator/templates/sdk/auth/useAuth.ts +++ b/waspc/data/Generator/templates/sdk/auth/useAuth.ts @@ -1,9 +1,9 @@ {{={= =}=}} import { deserialize as superjsonDeserialize } from 'superjson' import { useQuery } from 'wasp/rpc' -import api, { handleApiError } from 'wasp/api' +import { api, handleApiError } from 'wasp/client/api' import { HttpMethod } from 'wasp/types' -import type { User } from './types' +import type { User } from './types' import { addMetadataToQuery } from 'wasp/rpc/queries' export const getMe = createUserGetter() diff --git a/waspc/data/Generator/templates/sdk/operations/index.ts b/waspc/data/Generator/templates/sdk/operations/index.ts index 1da917cb2c..8ef076ee1f 100644 --- a/waspc/data/Generator/templates/sdk/operations/index.ts +++ b/waspc/data/Generator/templates/sdk/operations/index.ts @@ -1,4 +1,4 @@ -import api, { handleApiError } from 'wasp/api' +import { api, handleApiError } from 'wasp/client/api' import { HttpMethod } from 'wasp/types' import { serialize as superjsonSerialize, diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index cc568abb7d..8fb552438c 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -83,8 +83,6 @@ "./auth/helpers/*": "./dist/auth/helpers/*.jsx", {=! Used by our code, uncodumented (but accessible) for users. =} "./auth/pages/createAuthRequiredPage": "./dist/auth/pages/createAuthRequiredPage.jsx", - {=! Used by users, documented. =} - "./api": "./dist/api/index.js", {=! Used by our framework code (Websockets), undocumented (but accessible) for users. =} "./api/events": "./dist/api/events.js", {=! Used by users, documented. =} @@ -147,13 +145,29 @@ "./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx", {=! ================= NEW API HERE =================== =} - {=! Public: { type MyEntity1, type MyEntity2, ... } =} - "./server/api": "./dist/server/api/index.js" + {=! Public: { type MyEntity1, type MyEntity2, ... } =} + {=! Private: [] =} + "./server/api": "./dist/server/api/index.js", + {=! Public: { api } =} + {=! Private: [sdk] =} + "./client/api": "./dist/api/index.js" + }, + {=! + TypeScript doesn't care about the redirects we define above in "exports" field; those + are used only in runtime. TypeScript instead follows exact path as stated in an import when + trying to find the type declarations. Therefore, when "exports" redirect doesn't match the path + it redirects to, we need to also let TypeScript know about it, and that can be done with + `typesVersions` field below. + =} + "typesVersions": { + "*": { + "client/api": ["api/index.ts"] + } }, "license": "ISC", "include": [ "src/**/*" ], - {=& depsChunk =}, - {=& devDepsChunk =} + {=& depsChunk =}, + {=& devDepsChunk =} } diff --git a/waspc/data/Generator/templates/sdk/server/api/index.ts b/waspc/data/Generator/templates/sdk/server/api/index.ts index 1d2fd78d57..4f721fdcc2 100644 --- a/waspc/data/Generator/templates/sdk/server/api/index.ts +++ b/waspc/data/Generator/templates/sdk/server/api/index.ts @@ -14,8 +14,8 @@ import { {=/ shouldImportAuthenticatedApi =} } from '../_types' -// PUBLIC API +// PUBLIC API {=# apiRoutes =} export type {= typeName =}< P extends ExpressParams = ExpressParams, @@ -23,7 +23,7 @@ export type {= typeName =}< ReqBody = any, ReqQuery extends ExpressQuery = ExpressQuery, Locals extends Record = Record -> = +> = {=# usesAuth =} AuthenticatedApi< {=/ usesAuth =} @@ -41,5 +41,4 @@ export type {= typeName =}< ReqQuery, Locals > - {=/ apiRoutes =} diff --git a/waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx b/waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx index d650c1b80d..062aba19d8 100644 --- a/waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx +++ b/waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx @@ -2,7 +2,7 @@ import { createContext, useState, useEffect } from 'react' import { io, Socket } from 'socket.io-client' -import { getSessionId } from 'wasp/api' +import { getSessionId } from 'wasp/client/api' import { apiEventsEmitter } from 'wasp/api/events' import config from 'wasp/core/config' diff --git a/waspc/examples/todo-typescript/src/ChatPage.tsx b/waspc/examples/todo-typescript/src/ChatPage.tsx index c1d424e2c1..30f8c3191f 100644 --- a/waspc/examples/todo-typescript/src/ChatPage.tsx +++ b/waspc/examples/todo-typescript/src/ChatPage.tsx @@ -1,5 +1,5 @@ import React, { useEffect, useRef, useState } from 'react' -import api from 'wasp/api' +import { api } from 'wasp/client/api' import { useSocket, useSocketListener,