Skip to content

Commit

Permalink
feat!: update to latest open api (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz authored Jun 28, 2024
1 parent 4051d93 commit 73df541
Show file tree
Hide file tree
Showing 41 changed files with 13,229 additions and 15,795 deletions.
18 changes: 1 addition & 17 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,7 @@ $ PRODUCT=video yarn generate:open-api
$ PRODUCT=chat yarn generate:open-api:dev
```

### Fix issues in chat code

If you have updated the generated chat code you'll have to fix the following issues manually in the generated code:

- Add `/** @ts-expect-error */ ` (make sure to use this exact comment format otherwise they will be missing from `d.ts` files) for imports for `ImageSizeRequest`, `OnlyUserIDRequest` in the `gen/chat/FilesApi.ts` and `gen/chat/MessagesApi.ts` files
- Add `/** @ts-expect-error */ ` (make sure to use this exact comment format otherwise they will be missing from `d.ts` files) for duplicate exports in `gen/chat/index.ts`

### Validate that the generated code works

To check your work, run the following commands:

```
yarn start
yarn test
```

If these commands run fine, we're good to go.
The current chat open API contains some issues, to fix some of them apply the following commit manually after updating the chat open API: [87f8672f9c302138d2c19f07b9b5236e3ba98714](https://github.com/GetStream/stream-node/pull/42/commits/87f8672f9c302138d2c19f07b9b5236e3ba98714)

## Release (for Stream developers)

Expand Down
8 changes: 6 additions & 2 deletions __tests__/block-lists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import { beforeAll, describe, expect, it } from 'vitest';
import { v4 as uuidv4 } from 'uuid';
import { createTestClient } from './create-test-client';
import { StreamClient } from '../src/StreamClient';
import { BlockList } from '../src/gen/chat';
import {
CreateBlockListRequest,
CreateBlockListRequestTypeEnum,
} from '../src/gen/chat';

describe('block lists CRUD API', () => {
let client: StreamClient;
let blockList: BlockList;
let blockList: CreateBlockListRequest;

beforeAll(() => {
client = createTestClient();
blockList = {
name: 'streamnodetest-F1' + uuidv4(),
words: ['Ricciardo should retire'],
type: CreateBlockListRequestTypeEnum.WORD,
};
});

Expand Down
6 changes: 5 additions & 1 deletion __tests__/channel-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('channel types CRUD API', () => {
const response = await client.chat.createChannelType({
name: channelType,
automod: CreateChannelTypeRequestAutomodEnum.DISABLED,
automod_behavior: 'block',
max_message_length: 1200,
});

expect(response.name).toBe(channelType);
Expand All @@ -30,13 +32,15 @@ describe('channel types CRUD API', () => {
it('update', async () => {
const response = await client.chat.updateChannelType(channelType, {
automod: CreateChannelTypeRequestAutomodEnum.SIMPLE,
automod_behavior: 'block',
max_message_length: 1200,
});

expect(response.automod).toBe(CreateChannelTypeRequestAutomodEnum.SIMPLE);

const getResponse = await client.chat.getChannelType({ name: channelType });

// @ts-expect-error typing problem
// Property 'automod' does not exist on type 'Response'.
expect(getResponse.automod).toBe(
CreateChannelTypeRequestAutomodEnum.SIMPLE,
);
Expand Down
38 changes: 32 additions & 6 deletions __tests__/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,30 @@ describe('channel API', () => {

it('create', async () => {
const response = await channel.getOrCreate({
// Type error: Object literal may only specify known properties, and 'name' does not exist in type 'ChannelInput'.
data: { created_by_id: user.id, name: channelId },
});

expect(response.channel?.cid).toBe(`${channel.type}:${channel.id}`);
// Type error: Property 'name' does not exist on type 'ChannelResponse'
expect(response.channel?.name).toBe(channelId);
});

// it("create - without id", async () => {
// const channelWithoutId = client.chat.channel('messaging');
// const response = await channelWithoutId.getOrCreate({data: {created_by_id: user.id, members: [{user_id: user.id}, {user_id: user2.id}]}});
it('create - without id', async () => {
const channelWithoutId = client.chat.channel('messaging');
const response = await channelWithoutId.getOrCreate({
data: {
created_by_id: user.id,
// Type error: Type '{ user_id: string; }' is missing the following properties from type 'ChannelMember':
// banned, channel_role, created_at, notifications_muted, and 2 more.
members: [{ user_id: user.id }, { user_id: user2.id }],
},
});

// expect(response.channel?.cid).toBe(channelWithoutId.cid);
expect(response.channel?.cid).toBe(channelWithoutId.cid);

// await channelWithoutId.delete();
// });
await channelWithoutId.delete();
});

it('update', async () => {
const response = await channel.update({
Expand All @@ -69,6 +79,22 @@ describe('channel API', () => {
expect(response.channel?.cooldown).toBe(100);
});

it('queryChannels', async () => {
const unfilteredResponse = await client.chat.queryChannels();

expect(unfilteredResponse.channels.length).toBeGreaterThan(1);

const filteredResponse = await client.chat.queryChannels({
filter_conditions: { cid: channel.cid },
});

expect(filteredResponse.channels.length).toBe(1);

const channelFromResponse = filteredResponse.channels[0];

expect(channelFromResponse.channel?.name).toBe(channelId);
});

it('query members', async () => {
const response = await channel.queryMembers({
filter_conditions: {
Expand Down
8 changes: 5 additions & 3 deletions __tests__/devices-push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
import {
CreateDeviceRequest,
CreateDeviceRequestPushProviderEnum,
PushProviderRequest,
PushProvider,
} from '../src/gen/chat';
import { createTestClient } from './create-test-client';
import { StreamClient } from '../src/StreamClient';
Expand All @@ -20,9 +20,11 @@ describe('devices and push', () => {
push_provider_name: 'firebase',
user_id: user.id,
};
const pushProvider: PushProviderRequest = {
// Type '{ name: string; type: string; xiaomi_app_secret: string; xiaomi_package_name: string; }'
// is missing the following properties from type 'PushProvider': created_at, updated_at
const pushProvider: PushProvider = {
name: 'test-push-provider',
type: 'xiaomi' as any as number,
type: 'xiaomi',
xiaomi_app_secret: '',
xiaomi_package_name: '',
};
Expand Down
14 changes: 5 additions & 9 deletions __tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('messages API', () => {
channel = client.chat.channel('messaging', channelId);
await channel.getOrCreate({
data: {
// Type '{ id: string; }' is missing the following properties from type 'UserObject': banned, custom, online, role
created_by: { id: user.id },
members: [{ user }, { user: user2 }],
},
Expand All @@ -54,7 +55,7 @@ describe('messages API', () => {

messageId = response.message?.id;

const getResponse = await channel.getManyMessages({ ids: [messageId!] });
const getResponse = await channel.getManyMessages({ ids: [messageId] });

expect(getResponse.messages.length).toBe(1);
});
Expand All @@ -66,6 +67,7 @@ describe('messages API', () => {
const response = await channel.updateMessage(messageId!, {
message: {
text: 'https://getstream.io/',
// Property 'custom' is missing in type '{ image_url: string; }' but required in type 'Attachment'
attachments: [urlAttachment],
user_id: user.id,
},
Expand Down Expand Up @@ -99,6 +101,7 @@ describe('messages API', () => {
language: TranslateMessageRequestLanguageEnum.HU,
});

// Property 'message' does not exist on type 'MessageResponse'.
expect(response.message?.i18n?.hu_text).toBeDefined();
});

Expand Down Expand Up @@ -150,20 +153,13 @@ describe('messages API', () => {
expect(response.results).toBeDefined();
});

it('flag and unflag', async () => {
it('flag', async () => {
const response = await client.flag({
target_message_id: messageId!,
user_id: user.id,
});

expect(response.flag?.target_message_id).toBe(messageId!);

const unflagResponse = await client.unflag({
target_message_id: messageId!,
user_id: user.id,
});

expect(unflagResponse).toBeDefined();
});

it('truncate', async () => {
Expand Down
21 changes: 18 additions & 3 deletions __tests__/user-compat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,42 @@ import { StreamClient } from '../src/StreamClient';
describe('user-video compatibility API', () => {
let client: StreamClient;
const user = {
id: 'stream-node-test-user',
id: uuidv4(),
role: 'admin',
name: 'Test User for user API compatibily',
custom: {
note: 'compatibilty test',
},
};

beforeAll(async () => {
beforeAll(() => {
client = createTestClient();
await client.upsertUsers({
});

it('upsert user', async () => {
const user = {
id: uuidv4(),
role: 'admin',
name: 'Test User for user API compatibily',
custom: {
note: 'compatibilty test',
},
};

const response = await client.upsertUsers({
users: {
[user.id]: { ...user },
},
});

expect(response.users[user.id].custom.note).toBe('compatibilty test');
});

it('create call', async () => {
const call = client.video.call('default', uuidv4());
const response = await call.create({ data: { created_by: user } });

// Backend returns: {custom: { custom: { note: 'compatibilty test' } }}
expect(response.call.created_by.custom.note).toBe('compatibilty test');
expect(response.call.created_by.name).toBe(
'Test User for user API compatibily',
Expand Down
41 changes: 35 additions & 6 deletions __tests__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { beforeAll, describe, expect, it } from 'vitest';
import { v4 as uuidv4 } from 'uuid';
import { createTestClient } from './create-test-client';
import { StreamClient } from '../src/StreamClient';
import { UserObjectRequest } from '../src/gen/chat';
import { UserRequest } from '../src/gen/chat';

describe('user API', () => {
let client: StreamClient;
const userId = 'streamnodetest' + uuidv4();
const newUser: UserObjectRequest = {
const newUser: UserRequest = {
id: userId,
role: 'user',
custom: {
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('user API', () => {
it('create guest', async () => {
await client.updateAppSettings({ multi_tenant_enabled: false });

const guest: UserObjectRequest = {
const guest: UserRequest = {
id: uuidv4(),
custom: {
color: 'red',
Expand Down Expand Up @@ -132,14 +132,41 @@ describe('user API', () => {
expect(muteResponse.mute?.target?.id).toBe(newUser.id);

const unmuteResponse = await client.unmuteUser({
target_id: newUser.id,
target_ids: [],
target_ids: [newUser.id],
user_id: user.id,
});

expect(unmuteResponse).toBeDefined();
});

it('block and unblock', async () => {
const badUser: UserRequest = {
id: 'bad-alice',
name: 'Alice',
};
await client.upsertUsers({ users: { [badUser.id]: badUser } });

const blockResponse = await client.blockUsers({
blocked_user_id: badUser.id,
user_id: user.id,
});

expect(blockResponse.blocked_user_id).toBe(badUser.id);

const blockedUsers = await client.getBlockedUsers({ userId: user.id });

expect(
blockedUsers.blocks.find((b) => b.blocked_user_id === badUser.id),
).toBeDefined();

const unblockResponse = await client.unblockUsers({
blocked_user_id: badUser.id,
user_id: user.id,
});

expect(unblockResponse.duration).toBeDefined();
});

it('send custom event', async () => {
const response = await client.sendCustomEventToUser(newUser.id, {
type: 'my-custom-event',
Expand All @@ -155,7 +182,9 @@ describe('user API', () => {
id: newUser.id,
set: {
role: 'admin',
color: 'blue',
custom: {
color: 'blue',
},
},
unset: ['name'],
},
Expand Down
12 changes: 7 additions & 5 deletions generate-openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ if [ "$PRODUCT" == 'video' ] || [ "$PRODUCT" == 'all' ] ; then
PROTOCOL_REPO_DIR="../protocol"
fi
if [ "$FROM_REPO" == 'chat' ]; then
SCHEMA_FILE="$PROTOCOL_REPO_DIR/releases/video-openapi.yaml"
SCHEMA_FILE="$PROTOCOL_REPO_DIR/releases/v2/video-openapi.yaml"
else
SCHEMA_FILE="$PROTOCOL_REPO_DIR/openapi/video-openapi.yaml"
fi

if [ "$FROM_REPO" == 'chat' ]; then
# Generate the Coordinator OpenAPI schema
make -C $PROTOCOL_REPO_DIR video-openapi
make -C $PROTOCOL_REPO_DIR openapi
fi

OUTPUT_DIR="./src/gen/video"
Expand Down Expand Up @@ -64,14 +64,14 @@ if [ "$PRODUCT" == 'chat' ] || [ "$PRODUCT" == 'all' ]; then
PROTOCOL_REPO_DIR="../protocol"
fi
if [ "$FROM_REPO" == 'chat' ]; then
SCHEMA_FILE="$PROTOCOL_REPO_DIR/releases/chat-openapi.yaml"
SCHEMA_FILE="$PROTOCOL_REPO_DIR/releases/v2/chat-serverside-api.yaml"
else
SCHEMA_FILE="$PROTOCOL_REPO_DIR/openapi/chat-openapi.yaml"
fi

if [ "$FROM_REPO" == 'chat' ]; then
# Generate the Coordinator OpenAPI schema
make -C $PROTOCOL_REPO_DIR chat-openapi
make -C $PROTOCOL_REPO_DIR openapi
fi

OUTPUT_DIR="./src/gen/chat"
Expand All @@ -91,7 +91,9 @@ if [ "$PRODUCT" == 'chat' ] || [ "$PRODUCT" == 'all' ]; then
--additional-properties=supportsES6=true \
--additional-properties=modelPropertyNaming=original \
--additional-properties=enumPropertyNaming=UPPERCASE \
--additional-properties=withoutRuntimeChecks=true
--additional-properties=withoutRuntimeChecks=true \
--global-property=skipFormModel=false \
--skip-validate-spec

# Remove the generated API client, just keep the models
cp -r $TEMP_OUTPUT_DIR/ $OUTPUT_DIR
Expand Down
Loading

0 comments on commit 73df541

Please sign in to comment.